Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct Signal {
- amplitude: f32,
- phase: f32,
- frequency: f32
- }
- impl Signal {
- pub fn new(amplitude: f32, phase: f32, frequency: f32) -> Signal {
- // let's say phase can only be negative if frequency >= 10.
- if phase < 0.0 && frequency < 10.0 {
- panic!();
- }
- Signal {
- amplitude, phase, frequency
- }
- }
- }
- #[derive(Default)]
- pub struct SignalBuilder {
- amplitude: Option<f32>,
- phase: Option<f32>,
- frequency: Option<f32>
- }
- impl SignalBuilder {
- pub fn new() -> SignalBuilder {
- SignalBuilder::default()
- }
- pub fn frequency(self, new_frequency: f32) -> SignalBuilder {
- SignalBuilder {
- frequency: Some(new_frequency),
- // Copy the rest from the previous instance.
- ..self
- }
- }
- pub fn amplitude(self, new_amplitude: f32) -> SignalBuilder {
- SignalBuilder {
- amplitude: Some(new_amplitude),
- // Copy the rest from the previous instance.
- ..self
- }
- }
- pub fn phase(self, new_phase: f32) -> SignalBuilder {
- SignalBuilder {
- phase: Some(new_phase),
- // Copy the rest from the previous instance.
- ..self
- }
- }
- pub fn build(self) -> Signal {
- Signal::new(self.amplitude.unwrap(), self.phase.unwrap(), self.frequency.unwrap())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement