SHARE
TWEET

help rust

a guest Jan 18th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -main.rs---------------------------------------------------------------------------
  2. fn test_device(device: &mut dyn Device) {
  3.     println!("Tests with basic remote.");
  4.     let mut basic_remote = BasicRemote::new(device);
  5.     basic_remote.power();
  6.     device.print_status();
  7.  
  8.     println!("Tests with advanced remote.");
  9.     let mut advanced_remote = AdvancedRemote::new(device);
  10.     advanced_remote.basic_remote().power();
  11.     advanced_remote.mute(); // -> error[E0499]: cannot borrow `advanced_remote` as mutable more than once at a time
  12. }
  13.  
  14. fn main() {
  15.     test_device(&mut Tv::default());
  16. }
  17.  
  18. -advanced_remote.rs------------------------------------------------------------------
  19. pub struct AdvancedRemote<'a> {
  20.    basic_remote: BasicRemote<'a>,
  21. }
  22.  
  23. impl<'a> AdvancedRemote<'a> {
  24.     pub fn new(device: &'a mut dyn Device) -> Self {
  25.        Self {
  26.            basic_remote: BasicRemote::new(device),
  27.        }
  28.    }
  29.  
  30.    pub fn basic_remote(&mut self) -> &'a mut BasicRemote {
  31.         &mut self.basic_remote
  32.     }
  33.  
  34.     pub fn mute(&mut self) {
  35.         println!("Remote: mute");
  36.         self.basic_remote.device().set_volume(0);
  37.     }
  38. }
  39.  
  40. -basic_remote.rs--------------------------------------------------------------------
  41. pub struct BasicRemote<'a> {
  42.    device: &'a mut dyn Device,
  43. }
  44.  
  45. impl<'a> BasicRemote<'a> {
  46.     pub fn new(device: &'a mut dyn Device) -> Self {
  47.        Self { device }
  48.    }
  49.  
  50.    pub fn device(&mut self) -> &mut dyn Device {
  51.        self.device
  52.    }
  53. }
  54.  
  55. impl<'a> Remote for BasicRemote<'a> {
  56.    fn power(&mut self) {
  57.        println!("Remote: power toggle");
  58.        if self.device.is_enabled() {
  59.            self.device.disable()
  60.        } else {
  61.            self.device.enable()
  62.        }
  63.    }
  64.  
  65.    fn volume_down(&mut self) {
  66.        println!("Remote: volume down");
  67.        self.device.set_volume(self.device.volume() - 10);
  68.    }
  69.  
  70.    fn volume_up(&mut self) {
  71.        println!("Remote: volume up");
  72.        self.device.set_volume(self.device.volume() + 10);
  73.    }
  74.  
  75.    fn channel_down(&mut self) {
  76.        println!("Remote: channel down");
  77.        self.device.set_channel(self.device.channel() - 1);
  78.    }
  79.  
  80.    fn channel_up(&mut self) {
  81.        println!("Remote: channel up");
  82.        self.device.set_channel(self.device.channel() + 1);
  83.    }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top