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§

source§

impl LengthHint

source

pub fn undefined() -> Self

source

pub fn exact(n: usize) -> Self

write_to will use exactly n bytes.

source

pub fn at_least(n: usize) -> Self

write_to will use at least n bytes.

source

pub fn at_most(n: usize) -> Self

write_to will use at most n bytes.

source

pub fn between(n: usize, m: usize) -> Self

write_to will use between n and m bytes.

source

pub fn capacity(&self) -> usize

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())
}
source

pub fn is_zero(&self) -> bool

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

Trait Implementations§

source§

impl Add<usize> for LengthHint

source§

type Output = LengthHint

The resulting type after applying the + operator.
source§

fn add(self, other: usize) -> Self

Performs the + operation. Read more
source§

impl Add for LengthHint

source§

type Output = LengthHint

The resulting type after applying the + operator.
source§

fn add(self, other: LengthHint) -> Self

Performs the + operation. Read more
source§

impl AddAssign<usize> for LengthHint

source§

fn add_assign(&mut self, other: usize)

Performs the += operation. Read more
source§

impl AddAssign for LengthHint

source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
source§

impl BitOr for LengthHint

source§

fn bitor(self, other: LengthHint) -> Self

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);
source§

type Output = LengthHint

The resulting type after applying the | operator.
source§

impl BitOrAssign for LengthHint

source§

fn bitor_assign(&mut self, other: Self)

Performs the |= operation. Read more
source§

impl Clone for LengthHint

source§

fn clone(&self) -> LengthHint

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for LengthHint

source§

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

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

impl Mul<usize> for LengthHint

source§

type Output = LengthHint

The resulting type after applying the * operator.
source§

fn mul(self, other: usize) -> Self

Performs the * operation. Read more
source§

impl MulAssign<usize> for LengthHint

source§

fn mul_assign(&mut self, other: usize)

Performs the *= operation. Read more
source§

impl PartialEq for LengthHint

source§

fn eq(&self, other: &LengthHint) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Sum<usize> for LengthHint

source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = usize>,

Takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl Sum for LengthHint

source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = LengthHint>,

Takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl Copy for LengthHint

source§

impl Eq for LengthHint

source§

impl StructuralPartialEq for LengthHint

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.