Skip to content

Commit

Permalink
Add implementation notes doc comments for size_hint()
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Sikkema committed Apr 3, 2024
1 parent 4516ae4 commit 0191bd6
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/de/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ pub trait Flavor<'de>: 'de {
fn pop(&mut self) -> Result<u8>;

/// Returns the number of bytes remaining in the message, if known.
///
/// # Implementation notes
///
/// It is not enforced that this number is exactly correct.
/// A flavor may yield less or more bytes than the what is hinted at by
/// this function.
///
/// `size_hint()` is primarily intended to be used for optimizations such as
/// reserving space for deserialized items, but must not be trusted to
/// e.g., omit bounds checks in unsafe code. An incorrect implementation of
/// `size_hint()` should not lead to memory safety violations.
///
/// That said, the implementation should provide a correct estimation,
/// because otherwise it would be a violation of the trait’s protocol.
///
/// The default implementation returns `None` which is correct for any flavor.
fn size_hint(&self) -> Option<usize> {
None
}
Expand Down

0 comments on commit 0191bd6

Please sign in to comment.