pub trait SimpleAdapter {
type Item;
// Required method
fn adapt(&mut self, v: Vec<u8>) -> Self::Item;
}
Expand description
Simpler, less flexible, version of the Adapter
trait.
Can be used in situations where it suffices to just pass None
and Err()
values through and only operate when the inner
ByteChunker
’s .next()
returns Some(Ok(vec))
.
This is less powerful than just using
.map()
,
et. al., but simpler because there’s no error handling required by
the custom type.
The StringAdapter
type tracks error status, but we can implement a
simpler type that just performs lossy UTF-8 conversion.
use regex_chunker::{ByteChunker, SimpleAdapter};
use std::io::Cursor;
struct LossyStringAdapter {}
impl SimpleAdapter for LossyStringAdapter {
type Item = String;
fn adapt(&mut self, v: Vec<u8>) -> Self::Item {
String::from_utf8_lossy(&v).into()
}
}
let text = b"One, two, three four. Can I have a little more?";
let c = Cursor::new(text);
let chunks: Vec<_> = ByteChunker::new(c, "[ .,?]+")?
.with_simple_adapter(LossyStringAdapter{})
.map(|res| res.unwrap())
.collect();
assert_eq!(
&chunks,
&["One", "two", "three", "four", "Can", "I", "have", "a", "little", "more"].clone()
);
}
Required Associated Types§
Sourcetype Item
type Item
The type into which it converts the Vec<u8>
s successfully produced
by the underlying ByteChunker
’s Iterator
implementation.