Struct writeable::LengthHint

source ·
#[non_exhaustive]
pub struct LengthHint(pub usize, pub Option<usize>);
Expand description

A hint to help consumers of Writeable pre-allocate bytes before they call write_to.

This behaves like Iterator::size_hint: it is a tuple where the first element is the lower bound, and the second element is the upper bound. If the upper bound is None either there is no known upper bound, or the upper bound is larger than usize.

LengthHint implements std::ops::{Add, Mul} and similar traits for easy composition. During computation, the lower bound will saturate at usize::MAX, while the upper bound will become None if usize::MAX is exceeded.

Tuple Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§0: usize§1: Option<usize>

Implementations§

write_to will use exactly n bytes.

write_to will use at least n bytes.

write_to will use at most n bytes.

write_to will use between n and m bytes.

Returns a recommendation for the number of bytes to pre-allocate. If an upper bound exists, this is used, otherwise the lower bound (which might be 0).

Examples
use writeable::Writeable;

fn pre_allocate_string(w: &impl Writeable) -> String {
    String::with_capacity(w.writeable_length_hint().capacity())
}

Returns whether the LengthHint indicates that the string is exactly 0 bytes long.

Trait Implementations§

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Performs the += operation. Read more

Returns a new hint that is correct wherever self is correct, and wherever other is correct.

Example:


struct NonDeterministicWriteable(String, String);

impl Writeable for NonDeterministicWriteable {
    fn write_to<W: fmt::Write + ?Sized>(
        &self,
        sink: &mut W,
    ) -> fmt::Result {
        sink.write_str(if coin_flip() { &self.0 } else { &self.1 })
    }

    fn writeable_length_hint(&self) -> LengthHint {
        LengthHint::exact(self.0.len()) | LengthHint::exact(self.1.len())
    }
}

writeable::impl_display_with_writeable!(NonDeterministicWriteable);
The resulting type after applying the | operator.
Performs the |= operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
Performs the *= operation. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Method which takes an iterator and generates Self from the elements by “summing up” the items.
Method which takes an iterator and generates Self from the elements by “summing up” the items.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.