Guest User

Untitled

a guest
Feb 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 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. self.auth().map(f)
  22. }
  23.  
  24. pub fn auth(&mut self) -> Option<Admin<'_, D>> {
  25. if self.0.auth_admin() {
  26. Some(Admin(&mut self.0))
  27. } else {
  28. None
  29. }
  30. }
  31. }
  32.  
  33. impl<D: Device> Admin<'_, D> {
  34. pub fn user(&self) -> Option<User<'_, D>> {
  35. if self.0.auth_user() {
  36. Some(User(self.0))
  37. } else {
  38. None
  39. }
  40. }
  41. }
Add Comment
Please, Sign In to add comment