Skip to content

Commit 89bd260

Browse files
committed
Use #![deny(unsafe_op_in_unsafe_fn)]
1 parent c9e3652 commit 89bd260

File tree

7 files changed

+109
-90
lines changed

7 files changed

+109
-90
lines changed

examples/animation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use instant::Instant;
22
#[cfg(not(target_arch = "wasm32"))]
33
use rayon::prelude::*;
4-
use std::f64::consts::PI;
54
use softbuffer::GraphicsContext;
5+
use std::f64::consts::PI;
66
use winit::event::{Event, WindowEvent};
77
use winit::event_loop::{ControlFlow, EventLoop};
88
use winit::window::WindowBuilder;

src/cg.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@ impl CGImpl {
2323
let window = handle.ns_window as id;
2424
let view = handle.ns_view as id;
2525
let layer = CALayer::new();
26-
let subview: id = NSView::alloc(nil).initWithFrame_(NSView::frame(view));
27-
layer.set_contents_gravity(ContentsGravity::TopLeft);
28-
layer.set_needs_display_on_bounds_change(false);
29-
layer.set_contents_scale(window.backingScaleFactor());
30-
subview.setLayer(layer.id());
31-
subview.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);
26+
unsafe {
27+
let subview: id = NSView::alloc(nil).initWithFrame_(NSView::frame(view));
28+
layer.set_contents_gravity(ContentsGravity::TopLeft);
29+
layer.set_needs_display_on_bounds_change(false);
30+
layer.set_contents_scale(window.backingScaleFactor());
31+
subview.setLayer(layer.id());
32+
subview.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);
3233

33-
view.addSubview_(subview); // retains subview (+1) = 2
34-
let _: () = msg_send![subview, release]; // releases subview (-1) = 1
34+
view.addSubview_(subview); // retains subview (+1) = 2
35+
let _: () = msg_send![subview, release]; // releases subview (-1) = 1
36+
}
3537
Ok(Self { layer })
3638
}
3739

3840
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
3941
let color_space = CGColorSpace::create_device_rgb();
4042
let data =
41-
std::slice::from_raw_parts(buffer.as_ptr() as *const u8, buffer.len() * 4).to_vec();
43+
unsafe { std::slice::from_raw_parts(buffer.as_ptr() as *const u8, buffer.len() * 4) }
44+
.to_vec();
4245
let data_provider = CGDataProvider::from_buffer(Arc::new(data));
4346
let image = CGImage::new(
4447
width as usize,
@@ -52,6 +55,6 @@ impl CGImpl {
5255
false,
5356
kCGRenderingIntentDefault,
5457
);
55-
self.layer.set_contents(image.as_ptr() as id);
58+
unsafe { self.layer.set_contents(image.as_ptr() as id) };
5659
}
5760
}

src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![doc = include_str!("../README.md")]
2+
#![deny(unsafe_op_in_unsafe_fn)]
23

34
#[cfg(target_os = "macos")]
45
#[macro_use]
@@ -56,7 +57,7 @@ macro_rules! make_dispatch {
5657
match self {
5758
$(
5859
$(#[$attr])*
59-
Self::$name(inner) => inner.set_buffer(buffer, width, height),
60+
Self::$name(inner) => unsafe { inner.set_buffer(buffer, width, height) },
6061
)*
6162
}
6263
}
@@ -90,7 +91,7 @@ impl GraphicsContext {
9091
window: &W,
9192
display: &D,
9293
) -> Result<Self, SwBufError> {
93-
Self::from_raw(window.raw_window_handle(), display.raw_display_handle())
94+
unsafe { Self::from_raw(window.raw_window_handle(), display.raw_display_handle()) }
9495
}
9596

9697
/// Creates a new instance of this struct, using the provided raw window and display handles
@@ -108,22 +109,23 @@ impl GraphicsContext {
108109
(
109110
RawWindowHandle::Xlib(xlib_window_handle),
110111
RawDisplayHandle::Xlib(xlib_display_handle),
111-
) => Dispatch::X11(x11::X11Impl::new(xlib_window_handle, xlib_display_handle)?),
112+
) => Dispatch::X11(unsafe {
113+
x11::X11Impl::new(xlib_window_handle, xlib_display_handle)?
114+
}),
112115
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
113116
(
114117
RawWindowHandle::Wayland(wayland_window_handle),
115118
RawDisplayHandle::Wayland(wayland_display_handle),
116-
) => Dispatch::Wayland(wayland::WaylandImpl::new(
117-
wayland_window_handle,
118-
wayland_display_handle,
119-
)?),
119+
) => Dispatch::Wayland(unsafe {
120+
wayland::WaylandImpl::new(wayland_window_handle, wayland_display_handle)?
121+
}),
120122
#[cfg(target_os = "windows")]
121123
(RawWindowHandle::Win32(win32_handle), _) => {
122-
Dispatch::Win32(win32::Win32Impl::new(&win32_handle)?)
124+
Dispatch::Win32(unsafe { win32::Win32Impl::new(&win32_handle)? })
123125
}
124126
#[cfg(target_os = "macos")]
125127
(RawWindowHandle::AppKit(appkit_handle), _) => {
126-
Dispatch::CG(cg::CGImpl::new(appkit_handle)?)
128+
Dispatch::CG(unsafe { cg::CGImpl::new(appkit_handle)? })
127129
}
128130
#[cfg(target_arch = "wasm32")]
129131
(RawWindowHandle::Web(web_handle), _) => Dispatch::Web(web::WebImpl::new(web_handle)?),

src/orbital.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ impl OrbitalMap {
1515
let size = pages * syscall::PAGE_SIZE;
1616

1717
// Map window buffer
18-
let address = syscall::fmap(
19-
fd,
20-
&syscall::Map {
21-
offset: 0,
22-
size,
23-
flags: syscall::PROT_READ | syscall::PROT_WRITE,
24-
address: 0,
25-
},
26-
)?;
18+
let address = unsafe {
19+
syscall::fmap(
20+
fd,
21+
&syscall::Map {
22+
offset: 0,
23+
size,
24+
flags: syscall::PROT_READ | syscall::PROT_WRITE,
25+
address: 0,
26+
},
27+
)?
28+
};
2729

2830
Ok(Self { address, size })
2931
}
@@ -69,14 +71,17 @@ impl OrbitalImpl {
6971

7072
{
7173
// Map window buffer
72-
let window_map = OrbitalMap::new(window_fd, window_width * window_height * 4)
73-
.expect("failed to map orbital window");
74+
let window_map =
75+
unsafe { OrbitalMap::new(window_fd, window_width * window_height * 4) }
76+
.expect("failed to map orbital window");
7477

7578
// Window buffer is u32 color data in 0xAABBGGRR format
76-
let window_data = slice::from_raw_parts_mut(
77-
window_map.address as *mut u32,
78-
window_width * window_height,
79-
);
79+
let window_data = unsafe {
80+
slice::from_raw_parts_mut(
81+
window_map.address as *mut u32,
82+
window_width * window_height,
83+
)
84+
};
8085

8186
// Copy each line, cropping to fit
8287
let width = width_u16 as usize;

src/wayland/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ impl WaylandImpl {
2727
window_handle: WaylandWindowHandle,
2828
display_handle: WaylandDisplayHandle,
2929
) -> Result<Self, SwBufError> {
30-
let conn = Connection::from_backend(Backend::from_foreign_display(
31-
display_handle.display as *mut _,
32-
));
30+
// SAFETY: Ensured by user
31+
let backend = unsafe { Backend::from_foreign_display(display_handle.display as *mut _) };
32+
let conn = Connection::from_backend(backend);
3333
let (globals, event_queue) = unwrap(
3434
registry_queue_init(&conn),
3535
"Failed to make round trip to server",
@@ -40,10 +40,12 @@ impl WaylandImpl {
4040
"Failed to instantiate Wayland Shm",
4141
)?;
4242
let surface_id = unwrap(
43-
ObjectId::from_ptr(
44-
wl_surface::WlSurface::interface(),
45-
window_handle.surface as _,
46-
),
43+
unsafe {
44+
ObjectId::from_ptr(
45+
wl_surface::WlSurface::interface(),
46+
window_handle.surface as _,
47+
)
48+
},
4749
"Failed to create proxy for surface ID.",
4850
)?;
4951
let surface = unwrap(

src/win32.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Win32Impl {
4646
// Get the handle to the device context.
4747
// SAFETY: We have confirmed that the window handle is valid.
4848
let hwnd = handle.hwnd as HWND;
49-
let dc = GetDC(hwnd);
49+
let dc = unsafe { GetDC(hwnd) };
5050

5151
// GetDC returns null if there is a platform error.
5252
if dc == 0 {
@@ -61,7 +61,7 @@ impl Win32Impl {
6161

6262
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
6363
// Create a new bitmap info struct.
64-
let mut bitmap_info: BitmapInfo = mem::zeroed();
64+
let mut bitmap_info: BitmapInfo = unsafe { mem::zeroed() };
6565

6666
bitmap_info.bmi_header.biSize = mem::size_of::<BITMAPINFOHEADER>() as u32;
6767
bitmap_info.bmi_header.biPlanes = 1;
@@ -77,23 +77,25 @@ impl Win32Impl {
7777
// SAFETY:
7878
// - The bitmap information is valid.
7979
// - The buffer is a valid pointer to image data.
80-
StretchDIBits(
81-
self.dc,
82-
0,
83-
0,
84-
width as c_int,
85-
height as c_int,
86-
0,
87-
0,
88-
width as c_int,
89-
height as c_int,
90-
buffer.as_ptr().cast(),
91-
&bitmap_info as *const BitmapInfo as *const _,
92-
DIB_RGB_COLORS,
93-
SRCCOPY,
94-
);
80+
unsafe {
81+
StretchDIBits(
82+
self.dc,
83+
0,
84+
0,
85+
width as c_int,
86+
height as c_int,
87+
0,
88+
0,
89+
width as c_int,
90+
height as c_int,
91+
buffer.as_ptr().cast(),
92+
&bitmap_info as *const BitmapInfo as *const _,
93+
DIB_RGB_COLORS,
94+
SRCCOPY,
95+
)
96+
};
9597

9698
// Validate the window.
97-
ValidateRect(self.window, std::ptr::null_mut());
99+
unsafe { ValidateRect(self.window, std::ptr::null_mut()) };
98100
}
99101
}

src/x11.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ impl X11Impl {
6565
// it could mean either screen index zero, or that the screen number was not set. We
6666
// can't tell which, so we'll just assume that the screen number was not set.
6767
let screen = match display_handle.screen {
68-
0 => (lib.XDefaultScreen)(display_handle.display as *mut Display),
68+
0 => unsafe { (lib.XDefaultScreen)(display_handle.display as *mut Display) },
6969
screen => screen,
7070
};
7171

7272
// Use the default graphics context, visual and depth for this screen.
73-
let gc = (lib.XDefaultGC)(display_handle.display as *mut Display, screen);
74-
let visual = (lib.XDefaultVisual)(display_handle.display as *mut Display, screen);
75-
let depth = (lib.XDefaultDepth)(display_handle.display as *mut Display, screen);
73+
let gc = unsafe { (lib.XDefaultGC)(display_handle.display as *mut Display, screen) };
74+
let visual =
75+
unsafe { (lib.XDefaultVisual)(display_handle.display as *mut Display, screen) };
76+
let depth = unsafe { (lib.XDefaultDepth)(display_handle.display as *mut Display, screen) };
7677

7778
Ok(Self {
7879
window_handle,
@@ -86,35 +87,39 @@ impl X11Impl {
8687

8788
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
8889
// Create the image from the buffer.
89-
let image = (self.lib.XCreateImage)(
90-
self.display_handle.display as *mut Display,
91-
self.visual,
92-
self.depth as u32,
93-
ZPixmap,
94-
0,
95-
(buffer.as_ptr()) as *mut c_char,
96-
width as u32,
97-
height as u32,
98-
32,
99-
(width * 4) as i32,
100-
);
90+
let image = unsafe {
91+
(self.lib.XCreateImage)(
92+
self.display_handle.display as *mut Display,
93+
self.visual,
94+
self.depth as u32,
95+
ZPixmap,
96+
0,
97+
(buffer.as_ptr()) as *mut c_char,
98+
width as u32,
99+
height as u32,
100+
32,
101+
(width * 4) as i32,
102+
)
103+
};
101104

102105
// Draw the image to the window.
103-
(self.lib.XPutImage)(
104-
self.display_handle.display as *mut Display,
105-
self.window_handle.window,
106-
self.gc,
107-
image,
108-
0,
109-
0,
110-
0,
111-
0,
112-
width as c_uint,
113-
height as c_uint,
114-
);
106+
unsafe {
107+
(self.lib.XPutImage)(
108+
self.display_handle.display as *mut Display,
109+
self.window_handle.window,
110+
self.gc,
111+
image,
112+
0,
113+
0,
114+
0,
115+
0,
116+
width as c_uint,
117+
height as c_uint,
118+
)
119+
};
115120

116121
// Delete the image data.
117-
(*image).data = std::ptr::null_mut();
118-
(self.lib.XDestroyImage)(image);
122+
unsafe { (*image).data = std::ptr::null_mut() };
123+
unsafe { (self.lib.XDestroyImage)(image) };
119124
}
120125
}

0 commit comments

Comments
 (0)