Guest User

Untitled

a guest
Apr 27th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. fn main() {
  2. let name = String::from("World");
  3. let test = Test::new(&name);
  4. test.run().unwrap();
  5. }
  6.  
  7. struct Test<'a> {
  8. name: &'a String
  9. }
  10.  
  11. impl<'a> Test<'a> {
  12. fn new(name: &String) -> Test {
  13. Test { name }
  14. }
  15.  
  16. fn run(self) -> Result<Test<'a>, String> {
  17. ver_fn(Self::ver, box_fn(Self::greet))(self)
  18. }
  19.  
  20. fn ver(self) -> Result<Test<'a>, String> {
  21. match self.name.as_str() {
  22. "World" => Ok(self),
  23. _ => Err(String::from("some error"))
  24. }
  25. }
  26.  
  27. fn greet(self) -> Result<Test<'a>, String> {
  28. println!("Hello {}!", self.name);
  29. Ok(self)
  30. }
  31. }
  32.  
  33. 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>> {
  34. Box::new(move |a: A| -> Result<A, E> {
  35. match method(a) {
  36. Ok(a) => next(a),
  37. Err(e) => Err(e),
  38. }
  39. })
  40. }
  41.  
  42. fn box_fn<'a, A: 'a, E: 'a>(method: fn(A) -> Result<A, E>) -> Box<'a + Fn(A) -> Result<A, E>> {
  43. Box::new(move |a: A| -> Result<A, E> {
  44. method(a)
  45. })
  46. }
Add Comment
Please, Sign In to add comment