Skip to content

Commit 924be4f

Browse files
committed
use a OnceCell instead of an Option to avoid &mut
i was using the Option essentially as a OnceCell, calling only the `get_or_insert_with` function, but that still requires a &mut reference, even though i only returned a normal & ref. but a OnceCell actually has an equivalent function (`get_or_init`), that only needs a & ref.
1 parent 6f546e0 commit 924be4f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/cursor.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use smithay::{
55
utils::{Logical, Physical, Point, Transform},
66
wayland::compositor::with_states,
77
};
8-
use std::{collections::HashMap, fmt::Debug, io::Read, time::Duration};
8+
use std::{cell::OnceCell, collections::HashMap, fmt::Debug, io::Read, time::Duration};
99
use xcursor::{CursorTheme, parser::Image};
1010

1111
const FALLBACK_CURSOR_DATA: &[u8] = include_bytes!("../resources/cursor.rgba");
@@ -16,7 +16,7 @@ pub enum RenderCursor<'a> {
1616
hotspot: Point<i32, Logical>,
1717
surface: WlSurface,
1818
},
19-
Named(&'a mut XCursor),
19+
Named(&'a XCursor),
2020
}
2121

2222
#[derive(Debug)]
@@ -59,7 +59,7 @@ impl Cursor {
5959
self.cache.clear();
6060
}
6161

62-
fn get_named_cursor(&mut self, icon: CursorIcon) -> &mut XCursor {
62+
fn get_named_cursor(&mut self, icon: CursorIcon) -> &XCursor {
6363
self.cache.entry(icon).or_insert_with(|| {
6464
// todo scale
6565
let size = self.size;
@@ -122,20 +122,23 @@ fn load_cursor_theme(
122122
#[derive(Debug)]
123123
pub struct Frame {
124124
image: Image,
125-
buffer: Option<MemoryRenderBuffer>,
125+
buffer: OnceCell<MemoryRenderBuffer>,
126126
}
127127

128128
impl Frame {
129129
pub fn new(image: Image) -> Self {
130-
Frame { image, buffer: None }
130+
Frame {
131+
image,
132+
buffer: OnceCell::new(),
133+
}
131134
}
132135

133136
pub fn hotspot(&self) -> Point<i32, Physical> {
134137
Point::from((self.image.xhot as i32, self.image.yhot as i32))
135138
}
136139

137-
pub fn buffer(&mut self) -> &MemoryRenderBuffer {
138-
self.buffer.get_or_insert_with(|| {
140+
pub fn buffer(&self) -> &MemoryRenderBuffer {
141+
self.buffer.get_or_init(|| {
139142
MemoryRenderBuffer::from_slice(
140143
&self.image.pixels_rgba,
141144
Fourcc::Argb8888,
@@ -199,15 +202,15 @@ impl XCursor {
199202
}
200203
}
201204

202-
pub fn frame(&mut self, duration: Duration) -> &mut Frame {
205+
pub fn frame(&self, duration: Duration) -> &Frame {
203206
if self.animation_duration == 0 {
204-
return &mut self.frames[0];
207+
return &self.frames[0];
205208
}
206209

207210
let mut millis = duration.as_millis() as u32;
208211
millis %= self.animation_duration;
209212

210-
for frame in &mut self.frames {
213+
for frame in &self.frames {
211214
if millis < frame.image.delay {
212215
return frame;
213216
}

0 commit comments

Comments
 (0)