diff --git a/src/audio/capture.rs b/src/audio/capture.rs index 008c32d5..66a68938 100644 --- a/src/audio/capture.rs +++ b/src/audio/capture.rs @@ -372,6 +372,10 @@ pub fn is_available() -> bool { /// /// This function returns the name of the default audio capture device. /// If none is available, an empty string is returned. +/// +/// # Panics +/// +/// Panics on allocation failure. #[must_use] pub fn default_device() -> FBox { unsafe { @@ -383,6 +387,10 @@ pub fn default_device() -> FBox { /// /// This function returns a vector of strings, containing the names of all available /// audio capture devices. +/// +/// # Panics +/// +/// Panics on allocation failure. #[must_use] pub fn available_devices() -> FBox> { unsafe { diff --git a/src/audio/sound.rs b/src/audio/sound.rs index 01030431..b9777962 100644 --- a/src/audio/sound.rs +++ b/src/audio/sound.rs @@ -38,6 +38,10 @@ pub struct Sound<'buf> { /// Creation impl<'buf> Sound<'buf> { /// Create a new `Sound` + /// + /// # Panics + /// + /// Panics on allocation failure #[must_use] pub fn new() -> Self { let s = unsafe { ffi::audio::sfSound_new() }; diff --git a/src/audio/sound_buffer.rs b/src/audio/sound_buffer.rs index b82c44a5..ee4e9d96 100644 --- a/src/audio/sound_buffer.rs +++ b/src/audio/sound_buffer.rs @@ -147,7 +147,9 @@ impl SoundBuffer { /// Get the samples stored in the buffer /// - /// Panic if the sample count exceeds usize range + /// # Panics + /// + /// Panics if the sample count exceeds usize range #[must_use] pub fn samples(&self) -> &[i16] { let len: usize = self diff --git a/src/audio/sound_stream.rs b/src/audio/sound_stream.rs index e4b23cf2..2d3e2770 100644 --- a/src/audio/sound_stream.rs +++ b/src/audio/sound_stream.rs @@ -74,6 +74,10 @@ unsafe extern "C" fn seek_callback( impl<'a, S: SoundStream> SoundStreamPlayer<'a, S> { /// Create a new `SoundStreamPlayer` with the specified [`SoundStream`]. + /// + /// # Panics + /// + /// Panics if for some reason a `SoundStreamPlayer` can't be created. pub fn new(sound_stream: &'a mut S) -> Self { let channel_count = sound_stream.channel_count(); let sample_rate = sound_stream.sample_rate(); diff --git a/src/graphics/convex_shape.rs b/src/graphics/convex_shape.rs index adf88de5..ea39b677 100644 --- a/src/graphics/convex_shape.rs +++ b/src/graphics/convex_shape.rs @@ -30,6 +30,10 @@ impl<'s> ConvexShape<'s> { /// /// # Arguments /// * `points_count` - The number of point for the convex shape + /// + /// # Panics + /// + /// Panics if for some reason a `ConvexShape` can't be created. #[must_use] pub fn new(points_count: usize) -> ConvexShape<'s> { let shape = unsafe { ffi::sfConvexShape_new() }; @@ -64,6 +68,10 @@ impl<'s> ConvexShape<'s> { /// # Arguments /// * index - Index of the point to change, in range `[0 .. get_point_count() - 1]` /// * point - New position of the point + /// + /// # Panics + /// + /// Panics on out of bounds access pub fn set_point>(&mut self, index: usize, point: P) { assert!( index < self.point_count(), diff --git a/src/graphics/custom_shape.rs b/src/graphics/custom_shape.rs index a507934f..37aaa97c 100644 --- a/src/graphics/custom_shape.rs +++ b/src/graphics/custom_shape.rs @@ -55,6 +55,10 @@ impl<'s> CustomShape<'s> { /// /// # Arguments /// * points - Implementation of [`CustomShapePoints`] + /// + /// # Panics + /// + /// Panics if for some reason a `CustomShape` can't be created. #[must_use] pub fn new(points: Box) -> CustomShape<'s> { // SAFETY: Box::into_raw produces non-null pointer diff --git a/src/graphics/font.rs b/src/graphics/font.rs index d5a47d0c..a3a236e2 100644 --- a/src/graphics/font.rs +++ b/src/graphics/font.rs @@ -326,11 +326,10 @@ impl Font { /// * `character_size` - Character size, in pixels #[must_use] pub fn texture(&self, character_size: u32) -> &Texture { - unsafe { - ffi::sfFont_getTexture(self, character_size) - .as_ref() - .expect("sfFont_getTexture failed") - } + // # Safety + // + // `getTexture` returns a reference, which is never null or dangling. + unsafe { &*ffi::sfFont_getTexture(self, character_size) } } /// Tell whether the smooth filter is enabled or not. diff --git a/src/graphics/rc_sprite.rs b/src/graphics/rc_sprite.rs index 942e84f6..2376f1f6 100644 --- a/src/graphics/rc_sprite.rs +++ b/src/graphics/rc_sprite.rs @@ -35,6 +35,10 @@ pub struct RcSprite { impl RcSprite { /// Create a new sprite + /// + /// # Panics + /// + /// Panics if for some reason a `Sprite` can't be created. #[must_use] pub fn new() -> Self { let sp = unsafe { ffi::sfSprite_new() }; @@ -87,6 +91,10 @@ impl RcSprite { /// * `texture` - New texture /// * `reset_rect` - Should the texture rect be reset to the size /// of the new texture? + /// + /// # Panics + /// + /// Panics on [`std::rc::Rc`] related shenanigans. pub fn set_texture(&mut self, texture: &RcTexture, reset_rect: bool) { self.set_rc_texture(texture); #[expect(clippy::unwrap_used)] diff --git a/src/graphics/rc_text.rs b/src/graphics/rc_text.rs index c5bc5fc9..dff32d7b 100644 --- a/src/graphics/rc_text.rs +++ b/src/graphics/rc_text.rs @@ -95,6 +95,10 @@ impl RcText { /// Set the font of the `RcText` /// /// font - New [`RcFont`] + /// + /// # Panics + /// + /// Panics on [`std::rc::Rc`] related shenanigans. pub fn set_font(&mut self, font: &RcFont) { self.set_rc_font(font); unsafe { diff --git a/src/graphics/rectangle_shape.rs b/src/graphics/rectangle_shape.rs index 65e9c51b..c76262a9 100644 --- a/src/graphics/rectangle_shape.rs +++ b/src/graphics/rectangle_shape.rs @@ -22,6 +22,10 @@ pub struct RectangleShape<'s> { impl<'s> RectangleShape<'s> { /// Returns a new `RectangleShape`. + /// + /// # Panics + /// + /// Panics if a `RectangleShape` can't be created for some reason. #[must_use] pub fn new() -> RectangleShape<'s> { let rectangle = unsafe { ffi::sfRectangleShape_new() }; diff --git a/src/graphics/render_texture.rs b/src/graphics/render_texture.rs index a7ca98d3..60c0fbee 100644 --- a/src/graphics/render_texture.rs +++ b/src/graphics/render_texture.rs @@ -81,11 +81,10 @@ impl RenderTexture { /// Return the target texture #[must_use] pub fn texture(&self) -> &Texture { - unsafe { - ffi::sfRenderTexture_getTexture(self) - .as_ref() - .expect("sfRenderTexture_getTexture failed") - } + // # Safety + // + // `getTexture` returns a reference, which is never null or dangling + unsafe { &*ffi::sfRenderTexture_getTexture(self) } } /// Enable or disable the smooth filter on a render texture diff --git a/src/graphics/sprite.rs b/src/graphics/sprite.rs index 285ca7f0..d3ba76fd 100644 --- a/src/graphics/sprite.rs +++ b/src/graphics/sprite.rs @@ -28,6 +28,10 @@ pub struct Sprite<'s> { impl<'s> Sprite<'s> { /// Create a new sprite + /// + /// # Panics + /// + /// Panics if a new `Sprite` can't be created for some reason. #[must_use] pub fn new() -> Sprite<'s> { let sp = unsafe { ffi::sfSprite_new() }; diff --git a/src/graphics/texture.rs b/src/graphics/texture.rs index 089d8f9a..aa916e3b 100644 --- a/src/graphics/texture.rs +++ b/src/graphics/texture.rs @@ -119,7 +119,7 @@ impl Texture { /// Convenience method to easily create and load a `Texture` from a file. pub fn from_file(filename: &str) -> SfResult> { - let mut new = Self::new().expect("Failed to create texture"); + let mut new = Self::new()?; new.load_from_file(filename, IntRect::default())?; Ok(new) } diff --git a/src/system/input_stream.rs b/src/system/input_stream.rs index 0235d0bd..f8798af4 100644 --- a/src/system/input_stream.rs +++ b/src/system/input_stream.rs @@ -70,6 +70,10 @@ pub struct InputStream<'src, T> { impl<'src, T: Read + Seek> InputStream<'src, T> { /// Create a new input stream from a `Read + Seek` source. + /// + /// # Panics + /// + /// Panics if a new `InputStream` can't be created for some reason. pub fn new(stream: &'src mut T) -> InputStream<'src, T> { let user_data: *mut T = stream; unsafe { diff --git a/src/system/string.rs b/src/system/string.rs index dbc67fde..8c76fe35 100644 --- a/src/system/string.rs +++ b/src/system/string.rs @@ -21,6 +21,8 @@ impl SfStr { } /// Convert to a UTF-8 `String` from the Rust standard library. /// + /// # Panics + /// /// Panics if the string is not valid UTF-32. #[must_use] #[expect(clippy::unwrap_used)] diff --git a/src/window/joystick.rs b/src/window/joystick.rs index 3f9ee7f6..65d8cd13 100644 --- a/src/window/joystick.rs +++ b/src/window/joystick.rs @@ -143,6 +143,10 @@ decl_opaque! { } /// Get the joystick information. +/// +/// # Panics +/// +/// Panics if an `Identification` can't be created for some reason #[must_use] pub fn identification(joystick: u32) -> FBox { unsafe {