Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support no_std #15

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- test-linux-aarch64
- test-macos
- test-windows
- test-no-std
- lint
steps:
- run: exit 0
Expand Down Expand Up @@ -78,6 +79,23 @@ jobs:
cargo check
cargo test

test-no-std:
runs-on: [self-hosted, X64]
strategy:
matrix:
target:
- x86_64-unknown-none
- aarch64-unknown-none
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check no_std
run: |
rustup target add ${{ matrix.target }}
cargo check --target ${{ matrix.target }} --no-default-features

lint:
runs-on: [self-hosted, X64]

Expand Down
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ keywords = ["string", "str", "volo"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bytes = "1"
bytes = { version = "1", default-features = false }
serde = { version = "1", optional = true, default-features = false }
simdutf8 = { version = "0.1", features = ["aarch64_neon"] }
simdutf8 = { version = "0.1", default-features = false, features = [
"aarch64_neon",
] }
redis = { version = "0.26", optional = true, default-features = false }
itoa = { version = "1", optional = true }
ryu = { version = "1", optional = true }

[features]
default = ["std"]
std = ["serde/std"]
serde = ["dep:serde"]
std = ["bytes/std", "simdutf8/std", "serde?/std"]
serde = ["serde/alloc"]
serde-unsafe = ["serde"]
redis = ["dep:redis", "itoa", "ryu"]
redis = ["std", "dep:redis", "itoa", "ryu"]
redis-unsafe = ["redis"]

[dev-dependencies]
Expand Down
35 changes: 19 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]

use bytes::{Bytes, BytesMut};
use simdutf8::basic::{from_utf8, Utf8Error};
use std::{
borrow::{Borrow, Cow},
cmp::Ordering,
convert::Infallible,
fmt, hash, iter,
ops::Deref,
str::FromStr,
extern crate alloc;

use alloc::{
borrow::Cow,
string::{String, ToString},
sync::Arc,
vec::Vec,
};
use bytes::{Bytes, BytesMut};
use core::{
borrow::Borrow, cmp::Ordering, convert::Infallible, fmt, hash, iter, ops::Deref, str::FromStr,
};
use simdutf8::basic::{from_utf8, Utf8Error};

/// `FastStr` is a string type that try to avoid the cost of clone.
#[derive(Clone)]
Expand Down Expand Up @@ -65,7 +68,7 @@ impl FastStr {
/// `v` must be valid UTF-8.
#[inline]
pub unsafe fn new_u8_slice_unchecked(v: &[u8]) -> Self {
let s = unsafe { std::str::from_utf8_unchecked(v) };
let s = unsafe { core::str::from_utf8_unchecked(v) };
Self::new(s)
}

Expand Down Expand Up @@ -119,7 +122,7 @@ impl FastStr {
/// `b` must be valid UTF-8.
#[inline]
pub unsafe fn from_bytes_unchecked(b: Bytes) -> Self {
let s = std::str::from_utf8_unchecked(&b);
let s = core::str::from_utf8_unchecked(&b);
if Self::can_inline(s) {
return Self::new(s);
}
Expand Down Expand Up @@ -601,7 +604,7 @@ impl Repr {
/// The length of `s` must be <= `INLINE_CAP`.
unsafe fn new_inline_impl(s: &str) -> Self {
let mut buf = [0u8; INLINE_CAP];
std::ptr::copy_nonoverlapping(s.as_ptr(), buf.as_mut_ptr(), s.len());
core::ptr::copy_nonoverlapping(s.as_ptr(), buf.as_mut_ptr(), s.len());
Self::Inline { len: s.len(), buf }
}

Expand Down Expand Up @@ -665,11 +668,11 @@ impl Repr {
match self {
Self::Empty => "",
// Safety: this is guaranteed by the user when creating the `FastStr`.
Self::Bytes(bytes) => unsafe { std::str::from_utf8_unchecked(bytes) },
Self::Bytes(bytes) => unsafe { core::str::from_utf8_unchecked(bytes) },
Self::ArcStr(arc_str) => arc_str,
Self::ArcString(arc_string) => arc_string,
Self::StaticStr(s) => s,
Self::Inline { len, buf } => unsafe { std::str::from_utf8_unchecked(&buf[..*len]) },
Self::Inline { len, buf } => unsafe { core::str::from_utf8_unchecked(&buf[..*len]) },
}
}

Expand Down Expand Up @@ -709,7 +712,7 @@ impl Repr {
match self {
Self::Empty => Self::Empty,
// Safety: this is guaranteed by the user when creating the `FastStr`.
Self::Bytes(bytes) => unsafe { Self::new(std::str::from_utf8_unchecked(bytes)) },
Self::Bytes(bytes) => unsafe { Self::new(core::str::from_utf8_unchecked(bytes)) },
Self::ArcStr(arc_str) => Self::ArcStr(Arc::clone(arc_str)),
Self::ArcString(arc_string) => Self::ArcString(Arc::clone(arc_string)),
Self::StaticStr(s) => Self::StaticStr(s),
Expand Down Expand Up @@ -757,7 +760,7 @@ impl Repr {
s[sub_offset..sub_offset + sub_len].as_bytes(),
)),
Repr::StaticStr(s) => Self::StaticStr(unsafe {
std::str::from_utf8_unchecked(&s.as_bytes()[sub_offset..sub_offset + sub_len])
core::str::from_utf8_unchecked(&s.as_bytes()[sub_offset..sub_offset + sub_len])
}),
Repr::Inline { len: _, buf } => Self::Inline {
len: sub_len,
Expand Down
1 change: 1 addition & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::{string::String, vec::Vec};
use core::fmt;

#[cfg(not(feature = "serde-unsafe"))]
Expand Down
Loading