writeable

Struct 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 u8)

🔬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> 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.