Skip to main content

SplitRatio

Struct SplitRatio 

Source
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

Source

pub const BALANCED: Self

An even split. The value every balanced constructor uses.

Source

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.

Source

pub const fn get(self) -> f32

The refined value. Always finite and always within [MIN_RATIO, 1 - MIN_RATIO].

Trait Implementations§

Source§

impl Clone for SplitRatio

Source§

fn clone(&self) -> SplitRatio

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for SplitRatio

Source§

impl Debug for SplitRatio

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SplitRatio

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for SplitRatio

Source§

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.

Source§

impl From<f32> for SplitRatio

Source§

fn from(v: f32) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for SplitRatio

Source§

fn eq(&self, other: &SplitRatio) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for SplitRatio

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for SplitRatio

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.