Skip to main content

SizeHint

Trait SizeHint 

Source
#[doc(hidden)]
pub trait SizeHint { // Required methods fn lower_bound(&self) -> usize; fn upper_bound(&self) -> Option<usize>; // Provided method final fn size_hint(&self) -> (usize, Option<usize>) { ... } }
🔬This is a nightly-only experimental API. (core_io_internals)
Expand description

Internal trait used to allow for specialization in Read::size_hint and Iterator::size_hint.

All types implement this through a blanket default implementation returning a hint of (0, None).

Implementors should only provide lower_bound and upper_bound. size_hint is provided as a final method to enforce correctness.

Required Methods§

Source

fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)

Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation.

Through specialization, all types implement this method returning a default value of 0.

Implementations must ensure the returned value is less than or equal to the true element count.

Source

fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)

Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None.

Through specialization, all types implement this method returning a default value of None.

Implementations must ensure the returned value is greater than or equal to the true element count.

Provided Methods§

Source

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)

Returns an estimate for the number of elements this container like type contains.

This is a final method, and is guaranteed to return (self.lower_bound(), self.upper_bound()).

Without specialization, types implementing this trait will return (0, None).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl SizeHint for &[u8]

Source§

impl SizeHint for Empty

Source§

impl SizeHint for Repeat

Source§

impl<T, U> SizeHint for Chain<T, U>

Source§

impl<T: ?Sized> SizeHint for T

Source§

impl<T> SizeHint for &mut T

Source§

impl<T> SizeHint for Take<T>