3 releases (breaking)
Uses new Rust 2024
0.3.0 | Mar 28, 2025 |
---|---|
0.2.0 | Mar 27, 2025 |
0.1.0 | Mar 27, 2025 |
#411 in Data structures
330 downloads per month
12KB
71 lines
Unsure<T>
Enum
Unsure<T>
is a custom enum which is essentially
Option<T>
with an additional Reject
variant, for handling values that are deemed to be
wholly negative (such as a failure state or an invalid input).
Implementation Details
Derived Traits
Debug
: AllowsUnsure<T>
values to be formatted for debugging purposes.Clone
: AllowsUnsure<T>
values to be explicitly cloned with.clone()
Copy
: AllowsUnsure<T>
values to be copied implicitly, allowing duplication without.clone()
.PartialEq
: AllowsUnsure<T>
values to be compared for equality.Eq
: AllowsUnsure<T>
values to be compared for total equality.Hash
: AllowsUnsure<T>
values to be used as keys inHashMap
orHashSet
.PartialOrd
: AllowsUnsure<T>
values to be compared for partial ordering.Ord
: AllowsUnsure<T>
values to be compared for total ordering.
Variants
Reject
: An erroneous or unwanted value, used to mark a situation where an unsure state should be rejected or cancelled. This is the main reason for usingUnsure<T>
overOption<T>
- if you don't need to manage failure separately from absence, just stick withOption<T>
.Nothing
: No value, likeOption<T>
'sNone
, used to mark a situation where an unsure state should be ignored or skipped. Takes its name after Haskell'sNothing
variant of theMaybe
type.Just(T)
: Some value of typeT
, likeOption<T>
'sSome(T)
, used to confirm that an unsure state is valid and confirmed. Takes its name after Haskell'sJust a
variant of theMaybe
type.
Methods
is_reject(&self) -> bool
: Returnstrue
if the value isReject
,false
otherwise.is_nothing(&self) -> bool
: Returnstrue
if the value isNothing
,false
otherwise.is_just(&self) -> bool
: Returnstrue
if the value isJust(T)
,false
otherwise.as_ref(&self) -> Option<&T>
: Returns a reference to the value asSome(&T)
if it isJust(T)
,None
otherwise.as_mut(&mut self) -> Option<&mut T>
: Returns a mutable reference to the value asSome(&mut T)
if it isJust(T)
,None
otherwise.unwrap(self) -> T
: Returns the value asT
if it isJust(T)
, panics otherwise.unwrap_or(self, default: T) -> T
: Returns the value asT
if it isJust(T)
, otherwise returnsdefault
.unwrap_or_else(self, f: impl FnOnce() -> T) -> T
: Returns the value asT
if it isJust(T)
, otherwise callsf
and returns the result.unwrap_or_default(self) -> T
: Returns the value asT
if it isJust(T)
, returns the default if it isNothing
, panics if it isReject
.
From
From<Option<T>>
: Converts anOption<T>
to anUnsure<T>
.Some(T)
maps ontoJust(T)
,None
maps ontoNothing
. It is impossible to obtain aReject
from this conversion.
Installation
unsure-rs
(unsure
on crates.io) is a library crate, so you must add it to
an existing Rust project:
cargo add unsure
You can additionally type it out manually in Cargo.toml
, but using the CLI
is easier.
Usage
See the docs.rs for usage instructions and detailed explanations of the enum.