predisposition 1.0.0

Three-valued boolean types with different predispositions for the unknown case
Documentation
  • Coverage
  • 100%
    14 out of 14 items documented0 out of 1 items with examples
  • Size
  • Source code size: 38.69 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.7 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 16s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • bharxhav/predisposition
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • bharxhav

predisposition

Three-valued boolean types for Rust. Each extends bool with a third variant representing an unknown value, and differs in how that unknown is handled when converted to bool:

  • OptimisticBool — unknown resolves to true
  • PessimisticBool — unknown resolves to false
  • UncertainBool — unknown stays unknown (TryFrom<bool> fails, Kleene logic preserves None)

Installation

cargo add predisposition

Usage

use predisposition::{OptimisticBool, PessimisticBool};

// user hasn't set a dark-mode preference — API returned None
let preference: Option<bool> = None;

// optimist: absent preference means dark mode is on
let dark_mode = OptimisticBool::from(preference);
render_theme(bool::from(dark_mode)); // true

// pessimist: absent preference means dark mode is off
let dark_mode = PessimisticBool::from(preference);
render_theme(bool::from(dark_mode)); // false

With cargo add predisposition --features serde, uncertainty round-trips without being resolved:

use predisposition::UncertainBool;

// store the preference as-is — don't pick a side
let dark_mode = UncertainBool::from(None);
let json = serde_json::to_string(&dark_mode).unwrap(); // "none"

// later, deserialize back — still unknown
let restored: UncertainBool = serde_json::from_str(&json).unwrap();
let inner: Option<bool> = restored.into(); // None