Skip to content

Commit

Permalink
refactor: remove 1 level of indirection via &Vec -> &[T]
Browse files Browse the repository at this point in the history
Typically `&Vec<T>` 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).
  • Loading branch information
murlakatamenka committed Mar 13, 2024
1 parent 22d36a8 commit 00f0c24
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions libwayshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ impl WayshotConnection {
}

/// Fetch all accessible wayland outputs.
pub fn get_all_outputs(&self) -> &Vec<OutputInfo> {
&self.output_infos
pub fn get_all_outputs(&self) -> &[OutputInfo] {
self.output_infos.as_slice()
}

/// refresh the outputs, to get new outputs
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -502,8 +502,8 @@ impl WayshotConnection {
region_capturer: RegionCapturer,
cursor_overlay: bool,
) -> Result<DynamicImage> {
let outputs_capture_regions: &Vec<(OutputInfo, Option<EmbeddedRegion>)> =
&match region_capturer {
let outputs_capture_regions: Vec<(OutputInfo, Option<EmbeddedRegion>)> =
match region_capturer {
RegionCapturer::Outputs(ref outputs) => outputs
.iter()
.map(|output_info| (output_info.clone(), None))
Expand Down Expand Up @@ -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())?
Expand Down
4 changes: 2 additions & 2 deletions libwayshot/src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ impl From<&OutputInfo> for LogicalRegion {
}
}

impl TryFrom<&Vec<OutputInfo>> for LogicalRegion {
impl TryFrom<&[OutputInfo]> for LogicalRegion {
type Error = Error;

fn try_from(output_info: &Vec<OutputInfo>) -> std::result::Result<Self, Self::Error> {
fn try_from(output_info: &[OutputInfo]) -> std::result::Result<Self, Self::Error> {
let x1 = output_info
.iter()
.map(|output| output.logical_region.inner.position.x)
Expand Down

0 comments on commit 00f0c24

Please sign in to comment.