pub enum CharsCowsAuto<'a> {
U32U8(CharsCows<'a, u32, u8>),
U32U16(CharsCows<'a, u32, u16>),
U32U32(CharsCows<'a, u32, u32>),
U64U8(CharsCows<'a, u64, u8>),
U64U16(CharsCows<'a, u64, u16>),
U64U32(CharsCows<'a, u64, u32>),
}Expand description
Automatically selects the most memory-efficient CharsCows type based on data size.
Returns an enum that can hold any combination of offset/length types.
Variants§
U32U8(CharsCows<'a, u32, u8>)
U32U16(CharsCows<'a, u32, u16>)
U32U32(CharsCows<'a, u32, u32>)
U64U8(CharsCows<'a, u64, u8>)
U64U16(CharsCows<'a, u64, u16>)
U64U32(CharsCows<'a, u64, u32>)
Implementations§
Source§impl<'a> CharsCowsAuto<'a>
impl<'a> CharsCowsAuto<'a>
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 the most memory-efficient CharsCows based on data size and max word length.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "hello world";
let cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
// Automatically picks CharsCows<u32, u8> for small data
assert_eq!(cows.len(), 2);Creates the most memory-efficient CharsCows using a two-pass strategy.
First pass scans to find the maximum word length, then second pass builds
with optimal types. Requires Clone iterator for memory efficiency.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "hello world";
let cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(), // Clone iterator
Cow::Borrowed(data.as_bytes())
).unwrap();
assert_eq!(cows.len(), 2);Sourcepub fn get(&self, index: usize) -> Option<&str>
pub fn get(&self, index: usize) -> Option<&str>
Returns a reference to the string at the given index.
Sourcepub fn bytes_per_entry(&self) -> usize
pub fn bytes_per_entry(&self) -> usize
Returns the byte size per entry for the selected type combination.
Sourcepub fn type_name(&self) -> &'static str
pub fn type_name(&self) -> &'static str
Returns a string describing the selected type combination.
Sourcepub fn iter(&self) -> CharsCowsAutoIter<'_> ⓘ
pub fn iter(&self) -> CharsCowsAutoIter<'_> ⓘ
Returns an iterator over the string cows.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "hello world foo";
let cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
let words: Vec<&str> = cows.iter().collect();
assert_eq!(words, vec!["hello", "world", "foo"]);Sourcepub fn sort(&mut self)
pub fn sort(&mut self)
Sorts the slices in-place using the default string comparison.
This is a stable sort that preserves the order of equal elements.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "zebra apple banana";
let mut cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
cows.sort();
let sorted: Vec<&str> = cows.iter().collect();
assert_eq!(sorted, vec!["apple", "banana", "zebra"]);Sourcepub fn sort_unstable(&mut self)
pub fn sort_unstable(&mut self)
Sorts the slices in-place using an unstable sorting algorithm.
This is faster than stable sort but may not preserve the order of equal elements.
Sourcepub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
Sorts the slices in-place using a custom comparison function.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "aaa bb c";
let mut cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
// Sort by length, then alphabetically
cows.sort_by(|a, b| a.len().cmp(&b.len()).then(a.cmp(b)));
let sorted: Vec<&str> = cows.iter().collect();
assert_eq!(sorted, vec!["c", "bb", "aaa"]);Sourcepub fn sort_by_key<K, F>(&mut self, f: F)
pub fn sort_by_key<K, F>(&mut self, f: F)
Sorts the slices in-place using a key extraction function.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "aaa bb c";
let mut cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
// Sort by string length
cows.sort_by_key(|s| s.len());
let sorted: Vec<&str> = cows.iter().collect();
assert_eq!(sorted, vec!["c", "bb", "aaa"]);Sourcepub fn data(&self) -> &[u8] ⓘ
pub fn data(&self) -> &[u8] ⓘ
Returns a reference to the underlying data buffer.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "hello world";
let cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
assert_eq!(cows.data(), b"hello world");Sourcepub fn parent(&self) -> &str
pub fn parent(&self) -> &str
Returns a reference to the parent string that all slices reference.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "hello world";
let cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
assert_eq!(cows.parent(), "hello world");Sourcepub fn as_bytes(&self) -> BytesCowsAuto<'_>
pub fn as_bytes(&self) -> BytesCowsAuto<'_>
Returns a zero-copy view of this CharsCowsAuto as a BytesCowsAuto.
This is a no-cost operation that reinterprets the string collection as bytes without copying or moving any data.
§Examples
use stringtape::CharsCowsAuto;
use std::borrow::Cow;
let data = "hello world";
let cows = CharsCowsAuto::from_iter_and_data(
data.split_whitespace(),
Cow::Borrowed(data.as_bytes())
).unwrap();
let bytes = cows.as_bytes();
assert_eq!(bytes.get(0), Some(&b"hello"[..]));
assert_eq!(bytes.get(1), Some(&b"world"[..]));