1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Definition of the ListIndex type
//! 
use std::{convert::TryFrom, default::Default, fmt, num::NonZeroU32};

/// Vector index for the elements in the list. They are typically not
/// squential.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ListIndex {
    ndx: Option<NonZeroU32>
}

impl ListIndex {
    #[inline]
    pub fn new() -> ListIndex {
        Default::default()
    }
    #[inline]
    /// Returns `true` for a valid index.
    ///
    /// A valid index can be used in IndexList method calls.
    pub fn is_some(&self) -> bool {
        self.ndx.is_some()
    }
    #[inline]
    /// Returns `true` for an invalid index.
    ///
    /// An invalid index will always be ignored and have `None` returned from
    /// any IndexList method call that returns something.
    pub fn is_none(&self) -> bool {
        self.ndx.is_none()
    }
    #[inline]
    pub(crate) fn get(&self) -> Option<usize> {
        Some(self.ndx?.get() as usize - 1)
    }
    #[inline]
    pub(crate) fn set(mut self, index: Option<usize>) -> Self {
        if let Some(n) = index {
            self.ndx = NonZeroU32::try_from(n as u32 + 1).ok()
        }
        self
    }
}

impl From<u32> for ListIndex {
    fn from(index: u32) -> ListIndex {
        ListIndex::new().set(Some(index as usize))
    }
}

impl From<u64> for ListIndex {
    fn from(index: u64) -> ListIndex {
        ListIndex::new().set(Some(index as usize))
    }
}

impl From<usize> for ListIndex {
    fn from(index: usize) -> ListIndex {
        ListIndex::new().set(Some(index))
    }
}

impl From<Option<usize>> for ListIndex {
    fn from(index: Option<usize>) -> ListIndex {
        ListIndex::new().set(index)
    }
}

impl fmt::Display for ListIndex {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(ndx) = self.ndx {
            write!(f, "{}", ndx)
        } else {
            write!(f, "|")
        }
    }
}