Module malachite_base::unions
source · Expand description
Unions (sum types). These are essentially generic enums.
§unwrap
use malachite_base::union_struct;
use malachite_base::unions::UnionFromStrError;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
union_struct!(
(pub(crate)),
Union3,
Union3<T, T, T>,
[A, A, 'A', a],
[B, B, 'B', b],
[C, C, 'C', c]
);
let mut u: Union3<char, char, char>;
u = Union3::A('a');
assert_eq!(u.unwrap(), 'a');
u = Union3::B('b');
assert_eq!(u.unwrap(), 'b');
u = Union3::C('c');
assert_eq!(u.unwrap(), 'c');
§fmt
use malachite_base::union_struct;
use malachite_base::unions::UnionFromStrError;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
union_struct!(
(pub(crate)),
Union3,
Union3<T, T, T>,
[A, A, 'A', a],
[B, B, 'B', b],
[C, C, 'C', c]
);
let mut u: Union3<char, u32, bool>;
u = Union3::A('a');
assert_eq!(u.to_string(), "A(a)");
u = Union3::B(5);
assert_eq!(u.to_string(), "B(5)");
u = Union3::C(false);
assert_eq!(u.to_string(), "C(false)");
§from_str
use malachite_base::union_struct;
use malachite_base::unions::UnionFromStrError;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
union_struct!(
(pub(crate)),
Union3,
Union3<T, T, T>,
[A, A, 'A', a],
[B, B, 'B', b],
[C, C, 'C', c]
);
let u3: Union3<bool, u32, char> = Union3::from_str("B(5)").unwrap();
assert_eq!(u3, Union3::B(5));
let result: Result<Union3<char, u32, bool>, _> = Union3::from_str("xyz");
assert_eq!(result, Err(UnionFromStrError::Generic("xyz".to_string())));
let result: Result<Union3<char, u32, bool>, _> = Union3::from_str("A(ab)");
if let Err(UnionFromStrError::Specific(Union3::A(_e))) = result {
} else {
panic!("wrong error variant")
}
Modules§
- Iterators that generate unions without repetition.
Enums§
- This is a union, or sum type, of $n$ values. It is essentially a generic enum.
- This is the error type for the unions’
FromStr
implementations.