pub struct SplitRatio(/* private fields */);Expand description
The fraction of a split’s area allotted to side a, refined so that
the illegal values have no representation.
§Why a newtype and not a validated f32
The field used to be a public f32, which meant 0.0, 1.0, -3.0,
inf and NaN were all constructible and all deserialisable from the
wire. validate() rejected them — but validate() is called by a
caller who remembers to, and CBOR deserialisation writes the field
directly, so the guard was one forgotten call away from useless.
NaN was the worst of them because it fails silently in the wrong
direction. split_extent computes (total * ratio).round() and then
clamps — and f32::clamp returns NaN for a NaN input (it only
panics when the bounds are NaN), after which NaN as u16 saturates to
0. So a NaN ratio did not panic and did not error: side a silently
got zero cells and the pane vanished. The old comment on split_extent
claimed the clamp “pins NaN-free bounds”; it did not.
Now the only way in is SplitRatio::new, which normalises non-finite
input to the balanced default and clamps the rest into
[MIN_RATIO, 1 - MIN_RATIO]. The field is private, Deserialize routes
through the same constructor, and Serialize is transparent — so the
CBOR wire shape is byte-identical to the bare f32 it replaces.
Tier: truly-unrepresentable in-process (no constructor produces an out-of-range or non-finite value); parse-time-normalised on the wire.
Implementations§
Source§impl SplitRatio
impl SplitRatio
Sourcepub fn new(v: f32) -> Self
pub fn new(v: f32) -> Self
Refine an arbitrary f32.
Non-finite input (NaN, ±inf) becomes Self::BALANCED rather
than propagating: there is no sensible clamp for a value that is not
on the number line, and silently yielding a zero-width pane is the
bug this type exists to remove.
Trait Implementations§
Source§impl Clone for SplitRatio
impl Clone for SplitRatio
Source§fn clone(&self) -> SplitRatio
fn clone(&self) -> SplitRatio
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for SplitRatio
Source§impl Debug for SplitRatio
impl Debug for SplitRatio
Source§impl Default for SplitRatio
impl Default for SplitRatio
Source§impl<'de> Deserialize<'de> for SplitRatio
impl<'de> Deserialize<'de> for SplitRatio
Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Routes through SplitRatio::new, so a hostile or merely stale
peer cannot put an out-of-range or NaN ratio into a live tree.