Struct LazyConcat

Source
pub struct LazyConcat<'a, T, B>
where B: ?Sized + 'a + ToOwned,
{ /* private fields */ }

Implementations§

Source§

impl<'a, T, B> LazyConcat<'a, T, B>
where T: Concat<Cow<'a, B>> + Borrow<B> + Default + Length, B: ToOwned<Owned = T> + ?Sized + Length,

Source

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.

Source

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.

Source

pub fn normalize(&mut self)

Fully normalize the collection by concatenating every fragament onto the base.

Source

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.

Source

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.

Source

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);
Source

pub fn get_slice<R>(&self, range: R) -> &B
where R: RangeBounds<usize>, T: Sliceable<Slice = 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.

Source

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);
Source

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.

Source

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.

Source

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, using concat, 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());
Source§

impl<'a> LazyConcat<'a, String, str>

Source

pub fn chars<'b>(&'b self) -> impl Iterator<Item = char> + 'b

Creates an iterator over the chars of the String and any concatenated fragments. No normalization needs to be done for this to work.

Source

pub fn bytes<'b>(&'b self) -> impl Iterator<Item = u8> + 'b

Creates an iterator over the raw bytes of the String and any concatenated fragments. No normalization needs to be done for this to work.

Source§

impl<'a, I: Clone> LazyConcat<'a, Vec<I>, [I]>

Source

pub fn iter(&self) -> impl Iterator<Item = &I>

Creates an iterator over references to items of a Vec and any concatenated fragments. No normalization needs to be done for this to work.

Source

pub fn into_iter<'b>(&'b self) -> impl Iterator<Item = I> + 'b

Creates an iterator over the owned items of a Vec and any concatenated fragments. No normalization needs to be done for this to work.

Trait Implementations§

Source§

impl<'a, T, B> Debug for LazyConcat<'a, T, B>
where T: Concat<Cow<'a, B>> + Borrow<B> + Debug, B: ToOwned<Owned = T> + ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T, B> From<T> for LazyConcat<'a, T, B>
where T: Concat<Cow<'a, B>> + Borrow<B> + Default + Length, B: ToOwned<Owned = T> + ?Sized + Length,

Source§

fn from(base: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, T, B> Freeze for LazyConcat<'a, T, B>
where T: Freeze, B: ?Sized,

§

impl<'a, T, B> RefUnwindSafe for LazyConcat<'a, T, B>

§

impl<'a, T, B> Send for LazyConcat<'a, T, B>
where T: Send, <B as ToOwned>::Owned: Send, B: Sync + ?Sized,

§

impl<'a, T, B> Sync for LazyConcat<'a, T, B>
where T: Sync, <B as ToOwned>::Owned: Sync, B: Sync + ?Sized,

§

impl<'a, T, B> Unpin for LazyConcat<'a, T, B>
where T: Unpin, <B as ToOwned>::Owned: Unpin, B: ?Sized,

§

impl<'a, T, B> UnwindSafe for LazyConcat<'a, T, B>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.