ssz/
union_selector.rs

1use crate::*;
2
3/// Provides the one-byte "selector" from the SSZ union specification:
4///
5/// https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.3/ssz/simple-serialize.md#union
6#[derive(Copy, Clone)]
7pub struct UnionSelector(u8);
8
9impl From<UnionSelector> for u8 {
10    fn from(union_selector: UnionSelector) -> u8 {
11        union_selector.0
12    }
13}
14
15impl PartialEq<u8> for UnionSelector {
16    fn eq(&self, other: &u8) -> bool {
17        self.0 == *other
18    }
19}
20
21impl UnionSelector {
22    /// Instantiate `self`, returning an error if `selector > MAX_UNION_SELECTOR`.
23    pub fn new(selector: u8) -> Result<Self, DecodeError> {
24        Some(selector)
25            .filter(|_| selector <= MAX_UNION_SELECTOR)
26            .map(Self)
27            .ok_or(DecodeError::UnionSelectorInvalid(selector))
28    }
29}