Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let name = String::from("World");
- let test = Test::new(&name);
- test.run().unwrap();
- }
- struct Test<'a> {
- name: &'a String
- }
- impl<'a> Test<'a> {
- fn new(name: &String) -> Test {
- Test { name }
- }
- fn run(self) -> Result<Test<'a>, String> {
- ver_fn(Self::ver, box_fn(Self::greet))(self)
- }
- fn ver(self) -> Result<Test<'a>, String> {
- match self.name.as_str() {
- "World" => Ok(self),
- _ => Err(String::from("some error"))
- }
- }
- fn greet(self) -> Result<Test<'a>, String> {
- println!("Hello {}!", self.name);
- Ok(self)
- }
- }
- fn ver_fn<'a, A: 'a, E: 'a>(method: fn(A) -> Result<A, E>, next: Box<'a + Fn(A) -> Result<A, E>>) -> Box<'a + Fn(A) -> Result<A, E>> {
- Box::new(move |a: A| -> Result<A, E> {
- match method(a) {
- Ok(a) => next(a),
- Err(e) => Err(e),
- }
- })
- }
- fn box_fn<'a, A: 'a, E: 'a>(method: fn(A) -> Result<A, E>) -> Box<'a + Fn(A) -> Result<A, E>> {
- Box::new(move |a: A| -> Result<A, E> {
- method(a)
- })
- }
Add Comment
Please, Sign In to add comment