Skip to content

Commit 8e72885

Browse files
Simplify a couple of null checking code
1 parent c8bd841 commit 8e72885

21 files changed

+111
-282
lines changed

examples/borrowed-resources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn main() {
3232
sprite.set_scale2f(0.5, 0.5);
3333

3434
// Create a ConvexShape using the same texture.
35-
let mut convex_shape = ConvexShape::with_texture(&frank, 6);
35+
let mut convex_shape = ConvexShape::with_texture(6, &frank);
3636
convex_shape.set_point(0,
3737
&Vector2f {
3838
x: 400.0,

src/audio/sound.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,18 @@ impl<'s> Sound<'s> {
4747
/// Create a new Sound
4848
pub fn new() -> Sound<'s> {
4949
let s = unsafe { ffi::sfSound_create() };
50-
if s.is_null() {
51-
panic!("sfSound_create returned null.")
52-
} else {
53-
Sound {
54-
sound: s,
55-
buffer: PhantomData,
56-
}
50+
assert!(!s.is_null(), "Failed to create Sound");
51+
Sound {
52+
sound: s,
53+
buffer: PhantomData,
5754
}
5855
}
5956

6057
/// Create a new Sound with a buffer
6158
pub fn with_buffer(buffer: &SoundBufferRef) -> Sound {
62-
let s = unsafe { ffi::sfSound_create() };
63-
if s.is_null() {
64-
panic!("sfSound_create returned null.")
65-
} else {
66-
unsafe {
67-
ffi::sfSound_setBuffer(s, buffer as *const _ as _);
68-
}
69-
Sound {
70-
sound: s,
71-
buffer: PhantomData,
72-
}
73-
}
59+
let mut s = Sound::new();
60+
s.set_buffer(buffer);
61+
s
7462
}
7563

7664
/// Sets whether this sound should loop or not.
@@ -170,13 +158,10 @@ impl<'a> Default for Sound<'a> {
170158
impl<'s> Clone for Sound<'s> {
171159
fn clone(&self) -> Self {
172160
let s = unsafe { ffi::sfSound_copy(self.sound) };
173-
if s.is_null() {
174-
panic!("Sound is null");
175-
} else {
176-
Sound {
177-
sound: s,
178-
buffer: self.buffer,
179-
}
161+
assert!(!s.is_null(), "Failed to copy Sound");
162+
Sound {
163+
sound: s,
164+
buffer: self.buffer,
180165
}
181166
}
182167
}

src/audio/sound_buffer.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,8 @@ impl ToOwned for SoundBufferRef {
198198

199199
fn to_owned(&self) -> Self::Owned {
200200
let sound_buffer = unsafe { ffi::sfSoundBuffer_copy(self.raw()) };
201-
if sound_buffer.is_null() {
202-
panic!("Sound buffer is null");
203-
} else {
204-
SoundBuffer { sound_buffer: sound_buffer }
205-
}
201+
assert!(!sound_buffer.is_null(), "Failed to copy SoundBuffer");
202+
SoundBuffer { sound_buffer: sound_buffer }
206203
}
207204
}
208205

src/audio/sound_buffer_recorder.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ impl SoundBufferRecorder {
2020
/// Create a new sound buffer recorder
2121
pub fn new() -> SoundBufferRecorder {
2222
let buffer = unsafe { ffi::sfSoundBufferRecorder_create() };
23-
if buffer.is_null() {
24-
panic!("sfSoundBufferRecorder_create returned null.")
25-
} else {
26-
SoundBufferRecorder { sound_buffer_recorder: buffer }
27-
}
23+
assert!(!buffer.is_null(), "Failed to create SoundBufferRecorder");
24+
SoundBufferRecorder { sound_buffer_recorder: buffer }
2825
}
2926

3027
/// Start the capture of a sound buffer recorder
@@ -70,11 +67,8 @@ impl SoundBufferRecorder {
7067
/// Return Read-only access to the sound buffer
7168
pub fn buffer(&self) -> &SoundBufferRef {
7269
let buff = unsafe { ffi::sfSoundBufferRecorder_getBuffer(self.sound_buffer_recorder) };
73-
if buff.is_null() {
74-
panic!("sound_buffer_recorder::get_buffer: buffer was null");
75-
} else {
76-
unsafe { &*(buff as *const SoundBufferRef) }
77-
}
70+
assert!(!buff.is_null(), "sfSoundBufferRecorder_getBuffer failed");
71+
unsafe { &*(buff as *const SoundBufferRef) }
7872
}
7973

8074
/// Get the name of the current audio capture device.

src/graphics/circle_shape.rs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@ pub struct CircleShape<'s> {
1919
}
2020

2121
impl<'s> CircleShape<'s> {
22-
/// Create a new circle shape
23-
///
24-
/// Return Some(CircleShape) or None
22+
/// Creates a new circle shape.
2523
pub fn new() -> CircleShape<'s> {
2624
let circle = unsafe { ffi::sfCircleShape_create() };
27-
if circle.is_null() {
28-
panic!("sfCircleShape_create returned null.")
29-
} else {
30-
CircleShape {
31-
circle_shape: circle,
32-
texture: PhantomData,
33-
}
25+
assert!(!circle.is_null(), "Failed to create CircleShape");
26+
CircleShape {
27+
circle_shape: circle,
28+
texture: PhantomData,
3429
}
3530
}
3631

@@ -41,18 +36,9 @@ impl<'s> CircleShape<'s> {
4136
///
4237
/// Return Some(CircleShape) or None
4338
pub fn with_texture(texture: &'s TextureRef) -> CircleShape<'s> {
44-
let circle = unsafe { ffi::sfCircleShape_create() };
45-
if circle.is_null() {
46-
panic!("sfCircleShape_create returned null.")
47-
} else {
48-
unsafe {
49-
ffi::sfCircleShape_setTexture(circle, texture.raw(), sfTrue);
50-
}
51-
CircleShape {
52-
circle_shape: circle,
53-
texture: PhantomData,
54-
}
55-
}
39+
let mut shape = CircleShape::new();
40+
shape.set_texture(texture, true);
41+
shape
5642
}
5743

5844
/// Create a new CircleShape and initialize it.
@@ -63,19 +49,10 @@ impl<'s> CircleShape<'s> {
6349
///
6450
/// Default value on SFML are radius = 0 / pointCount = 30
6551
pub fn new_init(radius: f32, point_count: u32) -> CircleShape<'s> {
66-
let circle = unsafe { ffi::sfCircleShape_create() };
67-
if circle.is_null() {
68-
panic!("sfCircleShape_create returned null.")
69-
} else {
70-
unsafe {
71-
ffi::sfCircleShape_setRadius(circle, radius);
72-
ffi::sfCircleShape_setPointCount(circle, point_count as usize);
73-
}
74-
CircleShape {
75-
circle_shape: circle,
76-
texture: PhantomData,
77-
}
78-
}
52+
let mut shape = CircleShape::new();
53+
shape.set_radius(radius);
54+
shape.set_point_count(point_count);
55+
shape
7956
}
8057

8158
/// Set the radius of a circle

src/graphics/convex_shape.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,24 @@ impl<'s> ConvexShape<'s> {
3434
/// * points_count - The number of point for the convex shape
3535
pub fn new(points_count: u32) -> ConvexShape<'s> {
3636
let shape = unsafe { ffi::sfConvexShape_create() };
37-
if shape.is_null() {
38-
panic!("sfConvexShape_create returned null.")
39-
} else {
40-
unsafe {
41-
ffi::sfConvexShape_setPointCount(shape, points_count as usize);
42-
}
43-
ConvexShape {
44-
convex_shape: shape,
45-
texture: PhantomData,
46-
}
47-
}
37+
assert!(!shape.is_null(), "Failed to create ConvexShape");
38+
let mut shape = ConvexShape {
39+
convex_shape: shape,
40+
texture: PhantomData,
41+
};
42+
shape.set_point_count(points_count);
43+
shape
4844
}
4945

5046
/// Create a new convex shape with a texture
5147
///
5248
/// # Arguments
5349
/// * texture - The texture to apply to the convex shape
5450
/// * points_count - The number of point for the convex shape
55-
pub fn with_texture(texture: &'s TextureRef, points_count: u32) -> ConvexShape<'s> {
56-
let shape = unsafe { ffi::sfConvexShape_create() };
57-
if shape.is_null() {
58-
panic!("sfConvexShape_create returned null.")
59-
} else {
60-
unsafe {
61-
ffi::sfConvexShape_setTexture(shape, texture.raw(), sfTrue);
62-
ffi::sfConvexShape_setPointCount(shape, points_count as usize)
63-
}
64-
ConvexShape {
65-
convex_shape: shape,
66-
texture: PhantomData,
67-
}
68-
}
51+
pub fn with_texture(points_count: u32, texture: &'s TextureRef) -> ConvexShape<'s> {
52+
let mut shape = ConvexShape::new(points_count);
53+
shape.set_texture(texture, true);
54+
shape
6955
}
7056

7157
/// Set the position of a point.

src/graphics/custom_shape.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ impl<'s> CustomShape<'s> {
5858
Some(get_point_callback),
5959
raw_impl as *mut _)
6060
};
61-
if sp.is_null() {
62-
panic!("sfShape_create returned null.")
63-
} else {
64-
CustomShape {
65-
shape: sp,
66-
texture: PhantomData,
67-
points: raw_impl,
68-
}
61+
assert!(!sp.is_null(), "Failed to create CustomShape");
62+
CustomShape {
63+
shape: sp,
64+
texture: PhantomData,
65+
points: raw_impl,
6966
}
7067
}
7168

@@ -77,24 +74,9 @@ impl<'s> CustomShape<'s> {
7774
pub fn with_texture(points: Box<CustomShapePoints + Send>,
7875
texture: &'s TextureRef)
7976
-> CustomShape<'s> {
80-
let raw_impl = Box::into_raw(Box::new(points));
81-
let sp = unsafe {
82-
ffi::sfShape_create(Some(get_point_count_callback),
83-
Some(get_point_callback),
84-
raw_impl as *mut _)
85-
};
86-
if sp.is_null() {
87-
panic!("sfShape_create returned null.")
88-
} else {
89-
unsafe {
90-
ffi::sfShape_setTexture(sp, texture.raw(), sfTrue);
91-
}
92-
CustomShape {
93-
shape: sp,
94-
texture: PhantomData,
95-
points: raw_impl,
96-
}
97-
}
77+
let mut shape = Self::new(points);
78+
shape.set_texture(texture, true);
79+
shape
9880
}
9981

10082
/// Recompute the internal geometry of a shape

src/graphics/font.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,8 @@ impl Font {
157157
/// Return the texture
158158
pub fn texture(&mut self, character_size: u32) -> &TextureRef {
159159
let tex = unsafe { ffi::sfFont_getTexture(self.raw_mut(), character_size) };
160-
if tex.is_null() {
161-
panic!("Font::texture: texture is null");
162-
} else {
163-
unsafe { &*(tex as *const TextureRef) }
164-
}
160+
assert!(!tex.is_null(), "sfFont_getTexture failed");
161+
unsafe { &*(tex as *const TextureRef) }
165162
}
166163
}
167164

src/graphics/image.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ impl Image {
2727
/// * height - Height of the image
2828
pub fn new(width: u32, height: u32) -> Image {
2929
let image = unsafe { ffi::sfImage_create(width, height) };
30-
if image.is_null() {
31-
panic!("sfImage_create returned null.")
32-
} else {
33-
Image { image: image }
34-
}
30+
assert!(!image.is_null(), "Failed to create Image");
31+
Image { image: image }
3532
}
3633

3734
/// Create an image from a stream.

src/graphics/rectangle_shape.rs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,25 @@ impl<'s> RectangleShape<'s> {
2020
/// Returns a new `RectangleShape`.
2121
pub fn new() -> RectangleShape<'s> {
2222
let rectangle = unsafe { ffi::sfRectangleShape_create() };
23-
if rectangle.is_null() {
24-
panic!("sfRectangleShape_create returned null.")
25-
} else {
26-
RectangleShape {
27-
rectangle_shape: rectangle,
28-
texture: PhantomData,
29-
}
23+
assert!(!rectangle.is_null(), "Failed to create RectangleShape");
24+
RectangleShape {
25+
rectangle_shape: rectangle,
26+
texture: PhantomData,
3027
}
3128
}
3229

3330
/// Returns a new `RectangleShape` with the provided texture.
3431
pub fn with_texture(texture: &'s TextureRef) -> RectangleShape<'s> {
35-
let rectangle = unsafe { ffi::sfRectangleShape_create() };
36-
if rectangle.is_null() {
37-
panic!("sfRectangleShape_create returned null.")
38-
} else {
39-
unsafe {
40-
ffi::sfRectangleShape_setTexture(rectangle, texture.raw(), sfTrue);
41-
}
42-
RectangleShape {
43-
rectangle_shape: rectangle,
44-
texture: PhantomData,
45-
}
46-
}
32+
let mut shape = Self::new();
33+
shape.set_texture(texture, true);
34+
shape
4735
}
4836

4937
/// Returns a new `RectangleShape` with the provided size.
5038
pub fn with_size(size: &Vector2f) -> RectangleShape<'s> {
51-
let rectangle = unsafe { ffi::sfRectangleShape_create() };
52-
if rectangle.is_null() {
53-
panic!("sfRectangleShape_create returned null.")
54-
} else {
55-
unsafe {
56-
ffi::sfRectangleShape_setSize(rectangle, size.raw());
57-
}
58-
RectangleShape {
59-
rectangle_shape: rectangle,
60-
texture: PhantomData,
61-
}
62-
}
39+
let mut shape = Self::new();
40+
shape.set_size(size);
41+
shape
6342
}
6443

6544
/// Get the size of a rectangle shape

src/graphics/render_texture.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ impl RenderTexture {
5252
/// Return the target texture
5353
pub fn texture(&self) -> &TextureRef {
5454
let tex = unsafe { ffi::sfRenderTexture_getTexture(self.render_texture) };
55-
if tex.is_null() {
56-
panic!("RenderTexture::texture: Texture is null")
57-
} else {
58-
unsafe { &*(tex as *const TextureRef) }
59-
}
55+
assert!(!tex.is_null(), "sfRenderTexture_getTexture failed");
56+
unsafe { &*(tex as *const TextureRef) }
6057
}
6158

6259
/// Enable or disable the smooth filter on a render texture

0 commit comments

Comments
 (0)