From 14a485debad00bc6fc40b99d53e62e2b6a720e73 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Sun, 23 Jan 2022 18:14:26 +0000 Subject: [PATCH] outputs: Clean up for more Rust usage --- src/outputs.rs | 62 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/outputs.rs b/src/outputs.rs index 78297ec9..e4466457 100644 --- a/src/outputs.rs +++ b/src/outputs.rs @@ -125,7 +125,9 @@ pub mod c { pub fn get_state(&self) -> Option { let outputs = self.outputs.clone_ref(); let outputs = outputs.borrow(); - find_output(&outputs, self.wl_output.clone()).map(|o| o.current.clone()) + outputs + .find_output(self.wl_output.clone()) + .map(|o| o.current.clone()) } } @@ -151,7 +153,8 @@ pub mod c { let outputs = outputs.clone_ref(); let mut collection = outputs.borrow_mut(); let output_state: Option<&mut OutputState> - = find_output_mut(&mut collection, wl_output) + = collection + .find_output_mut(wl_output) .map(|o| &mut o.pending); match output_state { Some(state) => { state.transform = Some(transform) }, @@ -179,7 +182,8 @@ pub mod c { let outputs = outputs.clone_ref(); let mut collection = outputs.borrow_mut(); let output_state: Option<&mut OutputState> - = find_output_mut(&mut collection, wl_output) + = collection + .find_output_mut(wl_output) .map(|o| &mut o.pending); match output_state { Some(state) => { @@ -200,7 +204,8 @@ pub mod c { ) { let outputs = outputs.clone_ref(); let mut collection = outputs.borrow_mut(); - let output = find_output_mut(&mut collection, wl_output); + let output = collection + .find_output_mut(wl_output); match output { Some(output) => { output.current = output.pending.clone(); } None => log_print!( @@ -218,7 +223,8 @@ pub mod c { let outputs = outputs.clone_ref(); let mut collection = outputs.borrow_mut(); let output_state: Option<&mut OutputState> - = find_output_mut(&mut collection, wl_output) + = collection + .find_output_mut(wl_output) .map(|o| &mut o.pending); match output_state { Some(state) => { state.scale = factor; } @@ -272,28 +278,6 @@ pub mod c { } // TODO: handle unregistration - - fn find_output( - collection: &Outputs, - wl_output: WlOutput, - ) -> Option<&Output> { - collection.outputs - .iter() - .find_map(|o| - if o.output == wl_output { Some(o) } else { None } - ) - } - - fn find_output_mut( - collection: &mut Outputs, - wl_output: WlOutput, - ) -> Option<&mut Output> { - collection.outputs - .iter_mut() - .find_map(|o| - if o.output == wl_output { Some(o) } else { None } - ) - } } /// Generic size @@ -359,7 +343,11 @@ impl OutputState { } } -pub struct Output { +/// Not guaranteed to exist, +/// but can be used to look up state. +pub type OutputId = c::WlOutput; + +struct Output { output: c::WlOutput, pending: OutputState, current: OutputState, @@ -377,4 +365,22 @@ impl Outputs { sender, } } + + fn find_output(&self, wl_output: c::WlOutput) -> Option<&Output> { + self.outputs + .iter() + .find_map(|o| + if o.output == wl_output { Some(o) } else { None } + ) + } + + fn find_output_mut(&mut self, wl_output: c::WlOutput) + -> Option<&mut Output> + { + self.outputs + .iter_mut() + .find_map(|o| + if o.output == wl_output { Some(o) } else { None } + ) + } }