Guest User

Untitled

a guest
Jul 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. struct Service {
  2. is_admin: bool,
  3. is_authed: bool,
  4. is_logged_out: bool,
  5. }
  6.  
  7. impl Service {
  8.  
  9. fn authenticate<S>(&mut self, passwd: S) -> Result<(), String>
  10. where S: AsRef<str>
  11. {
  12. if passwd.as_ref() == "secretpassword" {
  13. self.is_authed = true;
  14. Ok(())
  15. } else {
  16. Err("Bad password".into())
  17. }
  18. }
  19.  
  20. fn escalate<S>(&mut self, passwd: S) -> Result<(), String>
  21. where S: AsRef<str>
  22. {
  23. if passwd.as_ref() == "adminpasswd" {
  24. self.is_admin = true;
  25. Ok(())
  26. } else {
  27. Err("Bad password".into())
  28. }
  29. }
  30.  
  31. fn perform_action(&self) {
  32. assert!(self.is_authed)
  33. println!("we are authed")
  34. }
  35.  
  36. fn perform_admin_action(&self) {
  37. assert!(self.is_admin)
  38. println!("we are admin")
  39. }
  40.  
  41. }
Add Comment
Please, Sign In to add comment