relp_num/traits/narrow.rs
1//! # Storing a wide value in a narrow type
2//!
3//! The direction [`Absorb`](crate::Absorb) does not go. That one reads a narrow value into a wide
4//! accumulator; this one writes a wide value back out into a narrow one. Both are written with the
5//! wide type as `Self`, for the reason set out there: an implementation names both types and can be
6//! written against what exists for the pair.
7//!
8//! Sizes come first. [`EncodingSize`](crate::EncodingSize) says how much room a value needs, and
9//! this says whether the type that was picked has it.
10
11/// Storing a value in a narrower type, exactly or not at all.
12///
13/// # Contract
14///
15/// [`narrow`](Narrow::narrow) returns `Some(n)` only when `n` is the same number, and `None`
16/// exactly when no value of the narrow type is. It never rounds and never wraps. Widening the
17/// result has to give back what was narrowed.
18///
19/// # What implements it
20///
21/// The rational types of this crate, into each of the fixed width rational types.
22///
23/// The table is deliberately smaller than [`Absorb`](crate::Absorb)'s, and not because the rest is
24/// unwanted. Narrowing into the markers of [`fixed`](crate::fixed) and into the primitive integers
25/// is the other half of what makes those types worth storing, and it is simply not written yet.
26/// The asymmetry is a gap, not a statement that those conversions do not make sense.
27pub trait Narrow<N> {
28 /// This value in the narrow type, if it fits.
29 #[must_use]
30 fn narrow(&self) -> Option<N>;
31}