Skip to content

Commit 1c0683c

Browse files
committed
Make GraphicsContext::window_mut() an unsafe fn.
As documented in the new safety comment, providing `&mut` access to an inner component about which there are consistency invariants is unsafe, because `&mut` is sufficient for a caller to completely replace the value (using assignment or `std::mem::swap()`). Luckily, when `softbuffer` is used with `winit`, no `&mut` access is needed. However, other windowing libraries such as `glfw` and `sdl2` do have `&mut` methods, so this method can't simply be removed. This is a breaking change since it makes a previously safe function unsafe, and should not be published without a major version bump (i.e. to `0.2.0` or higher).
1 parent 08883d7 commit 1c0683c

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,28 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
6262
})
6363
}
6464

65-
/// Gets shared access to the underlying window
65+
/// Gets shared access to the underlying window.
6666
#[inline]
6767
pub fn window(&self) -> &W {
6868
&self.window
6969
}
7070

71-
/// Gets mut/exclusive access to the underlying window
71+
/// Gets mut/exclusive access to the underlying window.
72+
///
73+
/// This method is `unsafe` because it could be used to replace the window with another one,
74+
/// thus dropping the original window and violating the property that this [`GraphicsContext`]
75+
/// will always be destroyed before the window it writes into. This method should only be used
76+
/// when the window type in use requires mutable access to perform some action on an existing
77+
/// window.
78+
///
79+
/// # Safety
80+
///
81+
/// - After the returned mutable reference is dropped, the window must still be the same window
82+
/// which this [`GraphicsContext`] was created for; and within that window, the
83+
/// platform-specific configuration for 2D drawing must not have been modified. (For example,
84+
/// on macOS the view hierarchy of the window must not have been modified.)
7285
#[inline]
73-
pub fn window_mut(&mut self) -> &mut W {
86+
pub unsafe fn window_mut(&mut self) -> &mut W {
7487
&mut self.window
7588
}
7689

0 commit comments

Comments
 (0)