Advertisement
Guest User

Untitled

a guest
Jul 11th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.43 KB | None | 0 0
  1.  
  2. pub struct Signal {
  3.     amplitude: f32,
  4.     phase: f32,
  5.     frequency: f32
  6. }
  7.  
  8. impl Signal {
  9.     pub fn new(amplitude: f32, phase: f32, frequency: f32) -> Signal {
  10.         // let's say phase can only be negative if frequency >= 10.
  11.         if phase < 0.0 && frequency < 10.0 {
  12.             panic!();
  13.         }
  14.  
  15.         Signal {
  16.             amplitude, phase, frequency
  17.         }
  18.     }
  19. }
  20.  
  21. #[derive(Default)]
  22. pub struct SignalBuilder {
  23.     amplitude: Option<f32>,
  24.     phase: Option<f32>,
  25.     frequency: Option<f32>
  26. }
  27.  
  28. impl SignalBuilder {
  29.     pub fn new() -> SignalBuilder {
  30.         SignalBuilder::default()
  31.     }
  32.  
  33.     pub fn frequency(self, new_frequency: f32) -> SignalBuilder {
  34.         SignalBuilder {
  35.             frequency: Some(new_frequency),
  36.             // Copy the rest from the previous instance.
  37.             ..self
  38.         }
  39.     }
  40.  
  41.     pub fn amplitude(self, new_amplitude: f32) -> SignalBuilder {
  42.         SignalBuilder {
  43.             amplitude: Some(new_amplitude),
  44.             // Copy the rest from the previous instance.
  45.             ..self
  46.         }
  47.     }
  48.  
  49.     pub fn phase(self, new_phase: f32) -> SignalBuilder {
  50.         SignalBuilder {
  51.             phase: Some(new_phase),
  52.             // Copy the rest from the previous instance.
  53.             ..self
  54.         }
  55.     }
  56.  
  57.     pub fn build(self) -> Signal {
  58.         Signal::new(self.amplitude.unwrap(), self.phase.unwrap(), self.frequency.unwrap())
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement