Guest User

Untitled

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::char;
  3.  
  4. enum PerlType {
  5. Int(i64),
  6. String(String),
  7. Nil,
  8. }
  9.  
  10. struct Wrap<T> {
  11. value: T,
  12. funs: HashMap<&'static str, fn(&mut T, &[PerlType]) -> PerlType>
  13. }
  14.  
  15. fn wrap_string(value: String) -> Wrap<String> {
  16. let mut t = Wrap {
  17. value,
  18. funs: HashMap::new(),
  19. };
  20.  
  21. t.funs.insert("len", |s, _args| {
  22. PerlType::Int(s.len() as i64)
  23. });
  24.  
  25. t.funs.insert("push", |s, args| {
  26. let ch = match args.get(0) {
  27. Some(PerlType::Int(n)) => n,
  28. _ => return PerlType::Nil,
  29. };
  30.  
  31. let ch = match char::from_u32(*ch as u32) {
  32. Some(ch) => ch,
  33. None => return PerlType::Nil,
  34. };
  35.  
  36. s.push(ch);
  37.  
  38. PerlType::Nil
  39. });
  40.  
  41. t
  42. }
  43.  
  44. fn main() {
  45.  
  46. }
Add Comment
Please, Sign In to add comment