Created
September 28, 2018 10:47
-
-
Save ElectricCoffee/90a9da239ec11a1a8c3ca255d45c08bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::marker::PhantomData; | |
| use std::convert::From; | |
| #[derive(Debug, Clone, Copy)] | |
| struct H; | |
| #[derive(Debug, Clone, Copy)] | |
| struct L; | |
| #[derive(Debug, Clone, Copy)] | |
| struct Sec<S, A> { | |
| _security_level: PhantomData<S>, | |
| data: A, | |
| } | |
| impl<S, A> Sec<S, A> { | |
| fn new(data: A) -> Sec<S, A> { | |
| Sec { data, _security_level: PhantomData } | |
| } | |
| fn open(self, _s: S) -> A { | |
| self.data | |
| } | |
| fn map<B, F>(self, f: F) -> Sec<S, B> where F: FnOnce(A) -> B { | |
| let data = f(self.data); | |
| Sec::new(data) | |
| } | |
| fn and_then<B, F>(self, f: F) -> Sec<S, B> where F: FnOnce(A) -> Sec<S, B> { | |
| f(self.data) | |
| } | |
| } | |
| impl<S, A> From<A> for Sec<S, A> { | |
| fn from(data: A) -> Sec<S, A> { | |
| Sec::new(data) | |
| } | |
| } | |
| fn main() { | |
| let a: Sec<H, i32> = Sec::new(12); | |
| println!("{:?}", a); | |
| let b = a.map(|x| x + 3); | |
| println!("{:?}", b); | |
| let c = b.and_then(|x| Sec::new(x + 3)); | |
| println!("{:?}", c); | |
| let d = c.open(H); | |
| println!("{:?}", d); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment