Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub trait Device {
- fn auth_admin(&self) -> bool { true }
- fn auth_user(&self) -> bool { true }
- }
- pub struct NoAuth<D: Device>(D);
- pub struct Device1(u32);
- impl Device for Device1 {}
- impl Device1 {
- pub fn new(arg: u32) -> NoAuth<Self> {
- NoAuth(Self(arg))
- }
- }
- pub struct Admin<'a, D: Device>(&'a mut D);
- pub struct User<'a, D: Device>(&'a D);
- impl<D: Device> NoAuth<D> {
- pub fn temp_auth<R, F: FnOnce(Admin<'_, D>) -> R>(&mut self, f: F) -> Option<R> {
- self.auth().map(f)
- }
- pub fn auth(&mut self) -> Option<Admin<'_, D>> {
- if self.0.auth_admin() {
- Some(Admin(&mut self.0))
- } else {
- None
- }
- }
- }
- impl<D: Device> Admin<'_, D> {
- pub fn user(&self) -> Option<User<'_, D>> {
- if self.0.auth_user() {
- Some(User(self.0))
- } else {
- None
- }
- }
- }
Add Comment
Please, Sign In to add comment