pub struct LazyConcat<'a, T, B>{ /* private fields */ }
Implementations§
Source§impl<'a, T, B> LazyConcat<'a, T, B>
impl<'a, T, B> LazyConcat<'a, T, B>
Sourcepub fn new(initial: T) -> Self
pub fn new(initial: T) -> Self
Construct a new LazyConcat
. The initial value should be an owned value, such as a Vec
or
a String
. This can be empty but it doesn’t have to be.
Sourcepub fn expecting_num_fragments(initial: T, n: usize) -> Self
pub fn expecting_num_fragments(initial: T, n: usize) -> Self
Construct a new LazyConcat
, but preallocate the vector of fragments with the expected number
of fragments, so that won’t need to be reallocated as fragments are added.
Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Fully normalize the collection by concatenating every fragament onto the base.
Sourcepub fn normalize_to_len(&mut self, len: usize) -> Option<usize>
pub fn normalize_to_len(&mut self, len: usize) -> Option<usize>
Normalize at least len
elements and return the number of elements that were actually normalized.
This could fail if there are not enough fragments to make up the required length, in which case
None
is returned and no work is done.
Sourcepub fn get_normalized_len(&self) -> usize
pub fn get_normalized_len(&self) -> usize
The amount of data (in bytes) that has already been normalized. This is the maximum length
of a slice that can be taken without first calling normalize
or
normalize_to_len
.
Sourcepub fn slice_needs_normalization<R: RangeBounds<usize>>(
&mut self,
range: R,
) -> bool
pub fn slice_needs_normalization<R: RangeBounds<usize>>( &mut self, range: R, ) -> bool
Checks if any normalization is required before taking a slice
§Examples
if lz.slice_needs_normalization(1..3) {
lz.normalize_to_len(4);
}
let slice = lz.get_slice(1..3);
Sourcepub fn get_slice<R>(&self, range: R) -> &B
pub fn get_slice<R>(&self, range: R) -> &B
Get a slice from the normalized data. Before calling this method you should check that the size of
the normalized data is sufficient to be able to support this slice and, if necessary normalizing
the data to the required size using normalize_to_len
.
§Panics
Panics when the range falls outside the size of the owned data.
Sourcepub fn done(self) -> T
pub fn done(self) -> T
Consume the LazyConcat, concatenate all of the fragments and return the owned, fully normalized data.
§Examples
let lz = LazyConcat::new(String::from("abc"))
.and_concat("def")
.and_concat("ghi");
let result: String = lz.done();
assert_eq!("abcdefghi", result);
Sourcepub fn and_concat<F: Into<Cow<'a, B>>>(self, fragment: F) -> Self
pub fn and_concat<F: Into<Cow<'a, B>>>(self, fragment: F) -> Self
Lazily concatenate an owned or borrowed fragment of data. No data will be moved or copied until the
next time that normalize
or normalize_to_len
is called.
This is the same as concat
except that it consumes and returns self, allowing for
method chaining.
Sourcepub fn concat<F: Into<Cow<'a, B>>>(&mut self, fragment: F)
pub fn concat<F: Into<Cow<'a, B>>>(&mut self, fragment: F)
Lazily concatenate an owned or borrowed fragment of data. No data will be moved or copied until the
next time that normalize
or normalize_to_len
is called.
Sourcepub fn split_normalized<'b>(
&'b mut self,
) -> (&'b B, ConcatOnly<&'b mut LazyConcat<'a, T, B>>)where
T: Sliceable<Slice = B>,
pub fn split_normalized<'b>(
&'b mut self,
) -> (&'b B, ConcatOnly<&'b mut LazyConcat<'a, T, B>>)where
T: Sliceable<Slice = B>,
Splits the LazyConcat
into two parts:
- An immutable borrow of the normalized concatenation of the root.
- A mutable view,
ConcatOnly
, which permits further lazy concatenation, usingconcat
, but no other mutation.
This lets you keep hold of a slice into the normalized root, while still allowing further concatenation
of fragments. This would not otherwise be possible because and_concat
consumes
self
and concat
takes &mut self
, preventing slices into
the normalized root to exist.
§Examples
let a = vec![0,1,2,3,4];
let mut lz = LazyConcat::new(Vec::new())
.and_concat(&a);
lz.normalize();
// New block scope to control the lifespan of the variables
{
let (norm, mut lz) = lz.split_normalized();
let slice = &norm[0..];
// Still possible to concatenate while holding a slice
lz.concat(vec![99]);
assert_eq!(vec![0,1,2,3,4], slice);
}
assert_eq!(vec![0,1,2,3,4,99], lz.done());