updated to include mutable borrows. Oops

This commit is contained in:
Nickiel12 2021-12-11 15:22:42 -08:00
parent ef8fbbd444
commit 4721366ec8
2 changed files with 28 additions and 30 deletions

View file

@ -44,16 +44,16 @@ impl StreamStates {
StreamStates{..Default::default()} StreamStates{..Default::default()}
} }
pub fn update(mut self, update: StateUpdate) -> Self { pub fn update(&mut self, update: StateUpdate) {
match update { match update {
StateUpdate::StreamRunning(new_val) => {self.stream_running = new_val; self}, StateUpdate::StreamRunning(new_val) => {self.stream_running = new_val;},
StateUpdate::StreamIsMuted(new_val) => {self.stream_is_muted = new_val; self}, StateUpdate::StreamIsMuted(new_val) => {self.stream_is_muted = new_val;},
StateUpdate::ComputerSoundIsOn(new_val) => {self.computer_sound_is_on = new_val; self}, StateUpdate::ComputerSoundIsOn(new_val) => {self.computer_sound_is_on = new_val;},
StateUpdate::ChangeSceneOnChangeSlideHotkey(new_val) => {self.change_scene_on_change_slide_hotkey = new_val; self}, StateUpdate::ChangeSceneOnChangeSlideHotkey(new_val) => {self.change_scene_on_change_slide_hotkey = new_val;},
StateUpdate::TimerCanRun(new_val) => {self.timer_can_run = new_val; self}, StateUpdate::TimerCanRun(new_val) => {self.timer_can_run = new_val;},
StateUpdate::TimerLength(new_val) => {self.timer_length = new_val; self}, StateUpdate::TimerLength(new_val) => {self.timer_length = new_val;},
StateUpdate::TimerText(new_val) => {self.timer_text = new_val; self}, StateUpdate::TimerText(new_val) => {self.timer_text = new_val;},
StateUpdate::Scene(new_val) => {self.change_scene(&new_val)}, StateUpdate::Scene(new_val) => {self.change_scene(&new_val)},
StateUpdate::SceneIsAugmented(new_val) => { StateUpdate::SceneIsAugmented(new_val) => {
self.scene_is_augmented = new_val; self.scene_is_augmented = new_val;
self.change_scene(&enums::Scenes::Augmented)}, self.change_scene(&enums::Scenes::Augmented)},
@ -72,7 +72,7 @@ impl StreamStates {
self.screen_sub_scene self.screen_sub_scene
} }
pub fn change_scene(mut self, scene: &enums::Scenes) -> Self { pub fn change_scene(&mut self, scene: &enums::Scenes) {
match scene { match scene {
enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight | enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight |
enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight
@ -80,19 +80,17 @@ impl StreamStates {
enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight | enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight |
enums::Scenes::ScreenWithLowerRight enums::Scenes::ScreenWithLowerRight
=> {StreamStates::set_screen_scene(self, scene)}, => {StreamStates::set_screen_scene(self, scene)},
enums::Scenes::Augmented => {self.current_scene = *scene; self} enums::Scenes::Augmented => {self.current_scene = *scene;}
} }
} }
fn set_camera_scene(mut self, scene: &enums::Scenes) -> Self{ fn set_camera_scene(&mut self, scene: &enums::Scenes) {
self.camera_sub_scene = scene.clone(); self.camera_sub_scene = scene.clone();
self.current_scene = scene.clone(); self.current_scene = scene.clone();
self
} }
fn set_screen_scene(mut self, scene: &enums::Scenes) -> Self { fn set_screen_scene(&mut self, scene: &enums::Scenes) {
self.screen_sub_scene = scene.clone(); self.screen_sub_scene = scene.clone();
self.current_scene = scene.clone(); self.current_scene = scene.clone();
self
} }
} }

View file

@ -51,37 +51,37 @@ fn scene_correctness(){
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraDefault); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraDefault);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::CameraWithUpperRight)); stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::CameraWithUpperRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithUpperRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithUpperRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithUpperRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithUpperRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::CameraWithLargeUpperRight)); stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::CameraWithLargeUpperRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLargeUpperRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::CameraWithLowerRight)); stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::CameraWithLowerRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraWithLowerRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
stream_state = stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::ScreenDefault)); stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::ScreenDefault));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenDefault);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight);
stream_state = stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::ScreenWithLowerRight)); stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::ScreenWithLowerRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithLowerRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithLowerRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithLowerRight); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithLowerRight);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraWithLowerRight);
stream_state = stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::ScreenWithUpperRight)); stream_state.update(StateUpdate::Scene(s_s::enums::Scenes::ScreenWithUpperRight));
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithUpperRight); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::ScreenWithUpperRight);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithUpperRight); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenWithUpperRight);
@ -94,35 +94,35 @@ fn test_updating() {
let mut stream_state = s_s::stream_states_class::StreamStates::new(); let mut stream_state = s_s::stream_states_class::StreamStates::new();
assert_eq!(stream_state.timer_can_run, true); assert_eq!(stream_state.timer_can_run, true);
stream_state = stream_state.update(StateUpdate::TimerCanRun(false)); stream_state.update(StateUpdate::TimerCanRun(false));
assert_eq!(stream_state.timer_can_run, false); assert_eq!(stream_state.timer_can_run, false);
assert_eq!(stream_state.timer_length, 15.0); assert_eq!(stream_state.timer_length, 15.0);
stream_state = stream_state.update(StateUpdate::TimerLength(7.5)); stream_state.update(StateUpdate::TimerLength(7.5));
assert_eq!(stream_state.timer_length, 7.5); assert_eq!(stream_state.timer_length, 7.5);
assert_eq!(stream_state.timer_text, "0.0"); assert_eq!(stream_state.timer_text, "0.0");
stream_state = stream_state.update(StateUpdate::TimerText(String::from("7.5"))); stream_state.update(StateUpdate::TimerText(String::from("7.5")));
assert_eq!(stream_state.timer_text, "7.5"); assert_eq!(stream_state.timer_text, "7.5");
assert_eq!(stream_state.stream_running, false); assert_eq!(stream_state.stream_running, false);
stream_state = stream_state.update(StateUpdate::StreamRunning(true)); stream_state.update(StateUpdate::StreamRunning(true));
assert_eq!(stream_state.stream_running, true); assert_eq!(stream_state.stream_running, true);
assert_eq!(stream_state.stream_is_muted, false); assert_eq!(stream_state.stream_is_muted, false);
stream_state = stream_state.update(StateUpdate::StreamIsMuted(true)); stream_state.update(StateUpdate::StreamIsMuted(true));
assert_eq!(stream_state.stream_is_muted, true); assert_eq!(stream_state.stream_is_muted, true);
assert_eq!(stream_state.computer_sound_is_on, true); assert_eq!(stream_state.computer_sound_is_on, true);
stream_state = stream_state.update(StateUpdate::ComputerSoundIsOn(false)); stream_state.update(StateUpdate::ComputerSoundIsOn(false));
assert_eq!(stream_state.computer_sound_is_on, false); assert_eq!(stream_state.computer_sound_is_on, false);
assert_eq!(stream_state.change_scene_on_change_slide_hotkey, true); assert_eq!(stream_state.change_scene_on_change_slide_hotkey, true);
stream_state = stream_state.update(StateUpdate::ChangeSceneOnChangeSlideHotkey(false)); stream_state.update(StateUpdate::ChangeSceneOnChangeSlideHotkey(false));
assert_eq!(stream_state.change_scene_on_change_slide_hotkey, false); assert_eq!(stream_state.change_scene_on_change_slide_hotkey, false);
assert_eq!(stream_state.scene_is_augmented, false); assert_eq!(stream_state.scene_is_augmented, false);
stream_state = stream_state.update(StateUpdate::SceneIsAugmented(true)); stream_state.update(StateUpdate::SceneIsAugmented(true));
assert_eq!(stream_state.scene_is_augmented, true); assert_eq!(stream_state.scene_is_augmented, true);
} }
@ -135,7 +135,7 @@ fn can_run_in_thread() {
let mut stream_state = s_s::stream_states_class::StreamStates::new(); let mut stream_state = s_s::stream_states_class::StreamStates::new();
for received in rx { for received in rx {
assert_eq!(received, StateUpdate::StreamRunning(true)); assert_eq!(received, StateUpdate::StreamRunning(true));
stream_state = stream_state.update(received); stream_state.update(received);
assert_eq!(stream_state.stream_running, true); assert_eq!(stream_state.stream_running, true);
break; break;
} }