Skip to main content

luminos_contracts/support/interacts_with_data/contains/
option.rs

1use std::option::Option;
2
3use super::super::{Contains, InteractsWithData, IntoData};
4
5impl<T> Contains<T> for Option<T> {
6    fn contains_item(&self, key: &T) -> bool {
7        self.is_some()
8    }
9}
10
11impl<T: Clone + PartialEq> InteractsWithData for Option<T> {
12    type Value = Option<T>;
13    type Item = T;
14
15    fn data(&self) -> &Self::Value {
16        self
17    }
18
19    fn exists(&self, key: &Self::Item) -> bool {
20        match self.data() {
21            Some(value) => value == key,
22            None => false,
23        }
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_option_interacts_with_data() {
33        let option = Some("a");
34
35        let expected = Some("a");
36        assert_eq!(option.all(), expected);
37        
38        assert!(option.exists(&"a")); 
39        assert!(!option.exists(&"c")); 
40    }
41}