Skip to content

Commit

Permalink
Add ULE impls to tinystr-neo
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jan 19, 2022
1 parent 146fcf5 commit fbdeb10
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions experimental/tinystr_neo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ all-features = true
[dependencies]
displaydoc = { version = "0.2.3", default-features = false }
serde = { version = "1.0.123", optional = true, default-features = false, features = ["alloc"] }
zerovec = { version = "0.5.0", path = "../../utils/zerovec", optional = true }

[dev-dependencies]
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
Expand Down
19 changes: 18 additions & 1 deletion experimental/tinystr_neo/src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ pub struct TinyAsciiStr<const N: usize> {

impl<const N: usize> TinyAsciiStr<N> {
pub const fn from_bytes(bytes: &[u8]) -> Result<Self, TinyStrError> {
Self::from_bytes_inner(bytes, false)
}

#[inline]
pub(crate) const fn from_bytes_inner(
bytes: &[u8],
allow_trailing_null: bool,
) -> Result<Self, TinyStrError> {
if bytes.len() > N {
return Err(TinyStrError::TooLarge {
max: N,
Expand All @@ -23,17 +31,26 @@ impl<const N: usize> TinyAsciiStr<N> {

let mut out = [0; N];
let mut i = 0;
let mut found_null = false;
while i < bytes.len() {
if bytes[i] == 0 {
return Err(TinyStrError::ContainsNull);
found_null = true;
} else if bytes[i] >= 0x80 {
return Err(TinyStrError::NonAscii);
} else if found_null {
// Error if there are contentful bytes after null
return Err(TinyStrError::ContainsNull);
}
out[i] = bytes[i];

i += 1;
}

if !allow_trailing_null && found_null {
// We found some trailing nulls, error
return Err(TinyStrError::ContainsNull);
}

Ok(Self { bytes: out })
}

Expand Down
3 changes: 3 additions & 0 deletions experimental/tinystr_neo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ mod error;
#[cfg(feature = "serde")]
mod serde;

#[cfg(feature = "zerovec")]
mod ule;

#[cfg(feature = "serde")]
extern crate alloc;

Expand Down

0 comments on commit fbdeb10

Please sign in to comment.