From 00f0c24b1d4eb84f89b450d9670527d792a6f6c8 Mon Sep 17 00:00:00 2001 From: "Sergey A." Date: Thu, 14 Mar 2024 01:34:17 +0300 Subject: [PATCH] refactor: remove 1 level of indirection via &Vec -> &[T] Typically `&Vec` isn't something you want when you need a read-only view over a Vec, because it adds another level of indirection: Vec already stores a pointer to heap-allocated buffer internally. Using slices `&[T]` removes such unnecessary level of indirection and is considered a cleaner design. It is cache friendlier and can be better optimized by the compiler (not that it should matter in this case). --- libwayshot/src/lib.rs | 14 +++++++------- libwayshot/src/region.rs | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/libwayshot/src/lib.rs b/libwayshot/src/lib.rs index c828054d..bc27c914 100644 --- a/libwayshot/src/lib.rs +++ b/libwayshot/src/lib.rs @@ -100,8 +100,8 @@ impl WayshotConnection { } /// Fetch all accessible wayland outputs. - pub fn get_all_outputs(&self) -> &Vec { - &self.output_infos + pub fn get_all_outputs(&self) -> &[OutputInfo] { + self.output_infos.as_slice() } /// refresh the outputs, to get new outputs @@ -414,7 +414,7 @@ impl WayshotConnection { Ok(frame_copies) } - fn overlay_frames(&self, frames: &Vec<(FrameCopy, FrameGuard, OutputInfo)>) -> Result<()> { + fn overlay_frames(&self, frames: &[(FrameCopy, FrameGuard, OutputInfo)]) -> Result<()> { let mut state = LayerShellState { configured_outputs: HashSet::new(), }; @@ -502,8 +502,8 @@ impl WayshotConnection { region_capturer: RegionCapturer, cursor_overlay: bool, ) -> Result { - let outputs_capture_regions: &Vec<(OutputInfo, Option)> = - &match region_capturer { + let outputs_capture_regions: Vec<(OutputInfo, Option)> = + match region_capturer { RegionCapturer::Outputs(ref outputs) => outputs .iter() .map(|output_info| (output_info.clone(), None)) @@ -542,10 +542,10 @@ impl WayshotConnection { .collect(), }; - let frames = self.capture_frame_copies(outputs_capture_regions, cursor_overlay)?; + let frames = self.capture_frame_copies(&outputs_capture_regions, cursor_overlay)?; let capture_region: LogicalRegion = match region_capturer { - RegionCapturer::Outputs(ref outputs) => outputs.try_into()?, + RegionCapturer::Outputs(outputs) => outputs.as_slice().try_into()?, RegionCapturer::Region(region) => region, RegionCapturer::Freeze(callback) => { self.overlay_frames(&frames).and_then(|_| callback())? diff --git a/libwayshot/src/region.rs b/libwayshot/src/region.rs index 879fc392..6813ded2 100644 --- a/libwayshot/src/region.rs +++ b/libwayshot/src/region.rs @@ -195,10 +195,10 @@ impl From<&OutputInfo> for LogicalRegion { } } -impl TryFrom<&Vec> for LogicalRegion { +impl TryFrom<&[OutputInfo]> for LogicalRegion { type Error = Error; - fn try_from(output_info: &Vec) -> std::result::Result { + fn try_from(output_info: &[OutputInfo]) -> std::result::Result { let x1 = output_info .iter() .map(|output| output.logical_region.inner.position.x)