Struct writeable::LengthHint
source · [−]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
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.0: usize
1: Option<usize>
Implementations
sourceimpl LengthHint
impl LengthHint
pub fn undefined() -> Self
sourcepub fn capacity(&self) -> usize
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())
}
Trait Implementations
sourceimpl Add<LengthHint> for LengthHint
impl Add<LengthHint> for LengthHint
type Output = Self
type Output = Self
The resulting type after applying the +
operator.
sourcefn add(self, other: LengthHint) -> Self
fn add(self, other: LengthHint) -> Self
Performs the +
operation. Read more
sourceimpl Add<usize> for LengthHint
impl Add<usize> for LengthHint
sourceimpl AddAssign<LengthHint> for LengthHint
impl AddAssign<LengthHint> for LengthHint
sourcefn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Performs the +=
operation. Read more
sourceimpl AddAssign<usize> for LengthHint
impl AddAssign<usize> for LengthHint
sourcefn add_assign(&mut self, other: usize)
fn add_assign(&mut self, other: usize)
Performs the +=
operation. Read more
sourceimpl BitOr<LengthHint> for LengthHint
impl BitOr<LengthHint> for LengthHint
sourcefn bitor(self, other: LengthHint) -> Self
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);
type Output = Self
type Output = Self
The resulting type after applying the |
operator.
sourceimpl BitOrAssign<LengthHint> for LengthHint
impl BitOrAssign<LengthHint> for LengthHint
sourcefn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
Performs the |=
operation. Read more
sourceimpl Clone for LengthHint
impl Clone for LengthHint
sourcefn clone(&self) -> LengthHint
fn clone(&self) -> LengthHint
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for LengthHint
impl Debug for LengthHint
sourceimpl Mul<usize> for LengthHint
impl Mul<usize> for LengthHint
sourceimpl MulAssign<usize> for LengthHint
impl MulAssign<usize> for LengthHint
sourcefn mul_assign(&mut self, other: usize)
fn mul_assign(&mut self, other: usize)
Performs the *=
operation. Read more
sourceimpl PartialEq<LengthHint> for LengthHint
impl PartialEq<LengthHint> for LengthHint
sourcefn eq(&self, other: &LengthHint) -> bool
fn eq(&self, other: &LengthHint) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &LengthHint) -> bool
fn ne(&self, other: &LengthHint) -> bool
This method tests for !=
.
sourceimpl Sum<LengthHint> for LengthHint
impl Sum<LengthHint> for LengthHint
sourcefn sum<I>(iter: I) -> Self where
I: Iterator<Item = LengthHint>,
fn sum<I>(iter: I) -> Self where
I: Iterator<Item = LengthHint>,
Method which takes an iterator and generates Self
from the elements by
“summing up” the items. Read more
sourceimpl Sum<usize> for LengthHint
impl Sum<usize> for LengthHint
impl Copy for LengthHint
impl Eq for LengthHint
impl StructuralEq for LengthHint
impl StructuralPartialEq for LengthHint
Auto Trait Implementations
impl RefUnwindSafe for LengthHint
impl Send for LengthHint
impl Sync for LengthHint
impl Unpin for LengthHint
impl UnwindSafe for LengthHint
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more