#[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
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
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§
Source§impl Add<usize> for LengthHint
impl Add<usize> for LengthHint
Source§impl Add for LengthHint
impl Add for LengthHint
Source§type Output = LengthHint
type Output = LengthHint
+
operator.Source§fn add(self, other: LengthHint) -> Self
fn add(self, other: LengthHint) -> Self
+
operation. Read moreSource§impl AddAssign<usize> for LengthHint
impl AddAssign<usize> for LengthHint
Source§fn add_assign(&mut self, other: usize)
fn add_assign(&mut self, other: usize)
+=
operation. Read moreSource§impl AddAssign for LengthHint
impl AddAssign for LengthHint
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moreSource§impl BitOr for LengthHint
impl BitOr for LengthHint
Source§fn 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);
Source§type Output = LengthHint
type Output = LengthHint
|
operator.Source§impl BitOrAssign for LengthHint
impl BitOrAssign for LengthHint
Source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
|=
operation. Read moreSource§impl Clone for LengthHint
impl Clone for LengthHint
Source§fn clone(&self) -> LengthHint
fn clone(&self) -> LengthHint
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for LengthHint
impl Debug for LengthHint
Source§impl Mul<usize> for LengthHint
impl Mul<usize> for LengthHint
Source§impl MulAssign<usize> for LengthHint
impl MulAssign<usize> for LengthHint
Source§fn mul_assign(&mut self, other: usize)
fn mul_assign(&mut self, other: usize)
*=
operation. Read moreSource§impl PartialEq for LengthHint
impl PartialEq for LengthHint
Source§impl Sum<usize> for LengthHint
impl Sum<usize> for LengthHint
Source§impl Sum for LengthHint
impl Sum for LengthHint
Source§fn sum<I>(iter: I) -> Selfwhere
I: Iterator<Item = LengthHint>,
fn sum<I>(iter: I) -> Selfwhere
I: Iterator<Item = LengthHint>,
Self
from the elements by “summing up”
the items.impl Copy for LengthHint
impl Eq for LengthHint
impl StructuralPartialEq for LengthHint
Auto Trait Implementations§
impl Freeze for LengthHint
impl RefUnwindSafe for LengthHint
impl Send for LengthHint
impl Sync for LengthHint
impl Unpin for LengthHint
impl UnwindSafe for LengthHint
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)