Struct LengthHint

struct LengthHint(251, 252)

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.

Implementations

impl LengthHint

fn undefined() -> Self
fn exact(n: usize) -> Self

write_to will use exactly n bytes.

fn at_least(n: usize) -> Self

write_to will use at least n bytes.

fn at_most(n: usize) -> Self

write_to will use at most n bytes.

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

write_to will use between n and m bytes.

fn capacity(self: &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())
}
fn is_zero(self: &Self) -> bool

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

impl Add for LengthHint

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

impl Add for LengthHint

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

impl AddAssign for LengthHint

fn add_assign(self: &mut Self, other: usize)

impl AddAssign for LengthHint

fn add_assign(self: &mut Self, other: Self)

impl BitOr for LengthHint

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

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

Example:

# use writeable::{LengthHint, Writeable};
# use core::fmt;
# fn coin_flip() -> bool { true }

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

impl BitOrAssign for LengthHint

fn bitor_assign(self: &mut Self, other: Self)

impl Clone for LengthHint

fn clone(self: &Self) -> LengthHint

impl Copy for LengthHint

impl Debug for LengthHint

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

impl Eq for LengthHint

impl Freeze for LengthHint

impl Mul for LengthHint

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

impl MulAssign for LengthHint

fn mul_assign(self: &mut Self, other: usize)

impl PartialEq for LengthHint

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

impl RefUnwindSafe for LengthHint

impl Send for LengthHint

impl StructuralPartialEq for LengthHint

impl Sum for LengthHint

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

impl Sum for LengthHint

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

impl Sync for LengthHint

impl Unpin for LengthHint

impl UnsafeUnpin for LengthHint

impl UnwindSafe for LengthHint

impl<T> Any for LengthHint

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for LengthHint

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for LengthHint

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

impl<T> CloneToUninit for LengthHint

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for LengthHint

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for LengthHint

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for LengthHint

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for LengthHint

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

impl<T, U> TryInto for LengthHint

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