pub struct BytesCows<'a, Offset: OffsetType = u32, Length: LengthType = u16> { /* private fields */ }Expand description
A memory-efficient collection of byte slices with configurable offset and length types.
Similar to CharsCows but for arbitrary binary data without UTF-8 validation.
Implementations§
Source§impl<'a, Offset: OffsetType, Length: LengthType> BytesCows<'a, Offset, Length>
impl<'a, Offset: OffsetType, Length: LengthType> BytesCows<'a, Offset, Length>
Sourcepub fn from_iter_and_data<I>(
iter: I,
data: Cow<'a, [u8]>,
) -> Result<Self, StringTapeError>
pub fn from_iter_and_data<I>( iter: I, data: Cow<'a, [u8]>, ) -> Result<Self, StringTapeError>
Creates BytesCows from iterator of byte slices and shared data buffer.
Slices must be subslices of the data buffer. Offsets and lengths are inferred from slice pointers.
Sourcepub fn from_offsets_and_data<I>(
iter: I,
data: Cow<'a, [u8]>,
) -> Result<Self, StringTapeError>
pub fn from_offsets_and_data<I>( iter: I, data: Cow<'a, [u8]>, ) -> Result<Self, StringTapeError>
Creates BytesCows from iterator of (offset, length) pairs and data buffer.
Sourcepub fn get(&self, index: usize) -> Option<&[u8]>
pub fn get(&self, index: usize) -> Option<&[u8]>
Returns a reference to the bytes at the given index, or None if out of bounds.
Sourcepub fn iter(&self) -> BytesCowsIter<'_, Offset, Length> ⓘ
pub fn iter(&self) -> BytesCowsIter<'_, Offset, Length> ⓘ
Returns an iterator over the byte cows.
Sourcepub fn as_chars(&self) -> Result<CharsCows<'_, Offset, Length>, StringTapeError>
pub fn as_chars(&self) -> Result<CharsCows<'_, Offset, Length>, StringTapeError>
Returns a zero-copy view of this BytesCows as a CharsCows if all slices are valid UTF-8.
This validates that all byte slices contain valid UTF-8, then reinterprets the collection as strings without copying or moving any data.
§Errors
Returns StringTapeError::Utf8Error if any slice contains invalid UTF-8.
§Examples
use stringtape::BytesCowsU32U8;
use std::borrow::Cow;
let data = b"hello world";
let bytes = BytesCowsU32U8::from_iter_and_data(
data.split(|&b| b == b' '),
Cow::Borrowed(&data[..])
).unwrap();
let chars = bytes.as_chars().unwrap();
assert_eq!(chars.get(0), Some("hello"));
assert_eq!(chars.get(1), Some("world"));