8 releases
Uses new Rust 2024
new 0.2.2 | May 18, 2025 |
---|---|
0.2.1 | May 18, 2025 |
0.1.4 | May 12, 2025 |
#385 in Text processing
470 downloads per month
14KB
230 lines
kaff_sso
kaff_sso
provides a generic fixed-capacity buffer with heap fallback for both small and large collections.
Features
- Inline storage up to 256 elements in
B8
..B256
variants. - Heap fallback in
Boxed
for buffers exceeding 256 elements. - Zero-copy access via:
as_slice
,as_ptr
orFrom<String>
with length>=256
- Implements
PartialEq
,Eq
,PartialOrd
, andOrd
based on buffer length. - UTF-8 specialization (
type UTF8 = Str<u8>
) with:Deref<Target = str>
andAsRef<str>
From<&str>
andFrom<String>
- Optional N-API integration (
feature = "napi"
):FromNapiValue
support for JavaScript strings.
Quick Start
Add to Cargo.toml
[dependencies]
kaff_sso = "0.1"
Basic Usage
use kaff_sso::Str;
// Inline small buffer
let s: Str<u8> = Str::from(&[1, 2, 3][..]);
assert_eq!(unsafe { s.as_slice() }, &[1, 2, 3]);
// UTF-8 string
use kaff_sso::UTF8;
let s = UTF8::from("hello");
assert_eq!(&*s, "hello");
Enabling N-API
kaff_sso = { version = "0.1", features = ["napi"] }
#[cfg(feature = "napi")]
use napi::FromNapiValue;
// Now UTF8 implements FromNapiValue
Safety
This crate exposes several unsafe
interfaces that require careful usage:
-
unsafe fn as_slice(&self) -> &[E]
-
Returns a raw slice constructed from a pointer and length. The caller must ensure:
- The buffer contents remain valid for the returned lifetime.
- No other mutable references exist while the slice is alive.
- The element type
E
has a valid bit-pattern in the firstlen()
positions.
-
-
unsafe fn as_mut_ptr(&mut self) -> *mut E
-
Provides an unchecked mutable pointer. The caller must ensure:
- Pointer arithmetic will not overflow or exceed the buffer bounds.
- No immutable references alias the same memory.
- Any writes via this pointer respect the valid bit-patterns for
E
.
-
-
impl AsRef<str>
andDeref<Target = str>
forUTF8
- Both use
mem::transmute
to cast a&[u8]
slice to&str
without revalidation. The user must guarantee that the contained bytes are valid UTF-8.
- Both use
-
From<UTF8> for String
-
Consumes the buffer via
String::from_raw_parts(ptr, len, len)
. Ensure that:- The
UTF8
instance holds a contiguous heap buffer (i.e. theBoxed
variant). - The memory allocation matches what
String
expects (pointer, length, capacity). - No double-drop occurs—once converted, do not use the original
UTF8
again.
- The
-
Best Practices
- Prefer the safe APIs (
as_slice
with prior validation viastd::str::from_utf8
) whenever possible. - Wrap
unsafe
calls in minimal scopes and document the invariants being upheld. - Do not mix safe and unsafe accesses that could violate Rust’s aliasing rules.
Dependencies
~0–9MB