pub struct Choice<const N: usize> { /* private fields */ }
Expand description
A Choice
represents a selection between several protocols offered by offer!
.
It wraps an ordinary non-negative number, with the guarantee that it is strictly less than the
type level number N
.
Unless you are implementing a backend
, you do not need to interact with
Choice
s directly. However, all backends must implement Transmit<Choice<N>, Val>
and Receive<Choice<N>>
for all N
in order to support the Choose
and Offer
constructs.
§Examples
It’s possible to construct a Choice
from all u8
strictly less than its type parameter N
:
use std::convert::TryInto;
use dialectic::backend::Choice;
let zero: Choice<3> = 0_u8.try_into()?;
let one: Choice<3> = 1_u8.try_into()?;
let two: Choice<3> = 2_u8.try_into()?;
assert_eq!(zero, 0_u8);
assert_eq!(one, 1_u8);
assert_eq!(two, 2_u8);
But we cannot construct a Choice
from a u8
equal to or greater than its type parameter
N
:
let attempted_three: Result<Choice<3>, _> = 3_u8.try_into();
let attempted_four: Result<Choice<3>, _> = 4_u8.try_into();
assert!(attempted_three.is_err());
assert!(attempted_four.is_err());
Note that this means Choice<0>
is unrepresentable, because you cannot choose something from a
set of zero things:
for i in 0 ..= u8::MAX {
let attempt: Result<Choice<0>, _> = i.try_into();
assert!(attempt.is_err());
}
Trait Implementations§
Source§impl<'de, const N: usize> Deserialize<'de> for Choice<N>
impl<'de, const N: usize> Deserialize<'de> for Choice<N>
Source§fn deserialize<D>(deserializer: D) -> Result<Choice<N>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Choice<N>, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<const N: usize> Ord for Choice<N>
impl<const N: usize> Ord for Choice<N>
Source§impl<const N: usize> PartialOrd for Choice<N>
impl<const N: usize> PartialOrd for Choice<N>
impl<const N: usize> Copy for Choice<N>
impl<const N: usize> Eq for Choice<N>
impl<const N: usize> StructuralPartialEq for Choice<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for Choice<N>
impl<const N: usize> RefUnwindSafe for Choice<N>
impl<const N: usize> Send for Choice<N>
impl<const N: usize> Sync for Choice<N>
impl<const N: usize> Unpin for Choice<N>
impl<const N: usize> UnwindSafe for Choice<N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more