Guest User

Untitled

a guest
Feb 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. pub trait Device {
  2. fn auth_admin(&self) -> bool { true }
  3. fn auth_user(&self) -> bool { true }
  4. }
  5.  
  6. pub struct NoAuth<D: Device>(D);
  7.  
  8. pub struct Device1(u32);
  9. impl Device for Device1 {}
  10. impl Device1 {
  11. pub fn new(arg: u32) -> NoAuth<Self> {
  12. NoAuth(Self(arg))
  13. }
  14. }
  15.  
  16. pub struct Admin<'a, D: Device>(&'a mut D);
  17. pub struct User<'a, D: Device>(&'a D);
  18.  
  19. impl<D: Device> NoAuth<D> {
  20. pub fn temp_auth<R, F: FnOnce(Admin<'_, D>) -> R>(&mut self, f: F) -> Option<R> {
  21. if self.0.auth_admin() {
  22. Some(f(Admin(&mut self.0)))
  23. } else {
  24. None
  25. }
  26. }
  27.  
  28. pub fn auth(&mut self) -> Option<Admin<'_, D>> {
  29. if self.0.auth_admin() {
  30. Some(Admin(&mut self.0))
  31. } else {
  32. None
  33. }
  34. }
  35. }
  36.  
  37. impl<D: Device> Admin<'_, D> {
  38. pub fn user(&self) -> Option<User<'_, D>> {
  39. if self.0.auth_user() {
  40. Some(User(self.0))
  41. } else {
  42. None
  43. }
  44. }
  45. }
Add Comment
Please, Sign In to add comment