mirror of
https://github.com/touchHLE/rust-sdl2.git
synced 2026-01-31 01:25:23 +01:00
fix use of deprecated {max,min}_value functions
and reduce heap allocations
This commit is contained in:
@@ -157,7 +157,7 @@ impl crate::EventSubsystem {
|
||||
/// Returns an error, if no more user events can be created.
|
||||
pub unsafe fn register_events(&self, nr: u32) -> Result<Vec<u32>, String> {
|
||||
let result = sys::SDL_RegisterEvents(nr as ::libc::c_int);
|
||||
const ERR_NR: u32 = ::std::u32::MAX - 1;
|
||||
const ERR_NR: u32 = u32::MAX - 1;
|
||||
|
||||
match result {
|
||||
ERR_NR => Err("No more user events can be created; SDL_LASTEVENT reached".to_owned()),
|
||||
@@ -436,8 +436,8 @@ impl DisplayEvent {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_ll(&self) -> (u8, i32) {
|
||||
match *self {
|
||||
fn to_ll(self) -> (u8, i32) {
|
||||
match self {
|
||||
DisplayEvent::None => (sys::SDL_DisplayEventID::SDL_DISPLAYEVENT_NONE as u8, 0),
|
||||
DisplayEvent::Orientation(orientation) => (
|
||||
sys::SDL_DisplayEventID::SDL_DISPLAYEVENT_ORIENTATION as u8,
|
||||
@@ -509,8 +509,8 @@ impl WindowEvent {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_ll(&self) -> (u8, i32, i32) {
|
||||
match *self {
|
||||
fn to_ll(self) -> (u8, i32, i32) {
|
||||
match self {
|
||||
WindowEvent::None => (0, 0, 0),
|
||||
WindowEvent::Shown => (1, 0, 0),
|
||||
WindowEvent::Hidden => (2, 0, 0),
|
||||
|
||||
@@ -19,7 +19,7 @@ impl Palette {
|
||||
// This is kind of a hack. We have to cast twice because
|
||||
// ncolors is a c_int, and validate_int only takes a u32.
|
||||
// FIXME: Modify validate_int to make this unnecessary
|
||||
let u32_max = u32::max_value() as usize;
|
||||
let u32_max = u32::MAX as usize;
|
||||
if capacity > u32_max {
|
||||
capacity = u32_max;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ use std::ptr;
|
||||
/// This value is smaller than strictly needed, but is useful in ensuring that
|
||||
/// rect sizes will never have to be truncated when clamping.
|
||||
pub fn max_int_value() -> u32 {
|
||||
i32::max_value() as u32 / 2
|
||||
i32::MAX as u32 / 2
|
||||
}
|
||||
|
||||
/// The minimal integer value that can be used for rectangle positions
|
||||
@@ -24,7 +24,7 @@ pub fn max_int_value() -> u32 {
|
||||
/// This value is needed, because otherwise the width of a rectangle created
|
||||
/// from a point would be able to exceed the maximum width.
|
||||
pub fn min_int_value() -> i32 {
|
||||
i32::min_value() / 2
|
||||
i32::MIN / 2
|
||||
}
|
||||
|
||||
fn clamp_size(val: u32) -> u32 {
|
||||
@@ -107,7 +107,7 @@ impl Rect {
|
||||
/// Creates a new rectangle from the given values.
|
||||
///
|
||||
/// The width and height are clamped to ensure that the right and bottom
|
||||
/// sides of the rectangle does not exceed i32::max_value() (the value
|
||||
/// sides of the rectangle does not exceed i32::MAX (the value
|
||||
/// 2147483647, the maximal positive size of an i32). This means that the
|
||||
/// rect size will behave oddly if you move it very far to the right or
|
||||
/// downwards on the screen.
|
||||
@@ -127,7 +127,7 @@ impl Rect {
|
||||
/// Creates a new rectangle centered on the given position.
|
||||
///
|
||||
/// The width and height are clamped to ensure that the right and bottom
|
||||
/// sides of the rectangle does not exceed i32::max_value() (the value
|
||||
/// sides of the rectangle does not exceed i32::MAX (the value
|
||||
/// 2147483647, the maximal positive size of an i32). This means that the
|
||||
/// rect size will behave oddly if you move it very far to the right or
|
||||
/// downwards on the screen.
|
||||
@@ -177,19 +177,19 @@ impl Rect {
|
||||
}
|
||||
|
||||
/// Sets the horizontal position of this rectangle to the given value,
|
||||
/// clamped to be less than or equal to i32::max_value() / 2.
|
||||
/// clamped to be less than or equal to i32::MAX / 2.
|
||||
pub fn set_x(&mut self, x: i32) {
|
||||
self.raw.x = clamp_position(x);
|
||||
}
|
||||
|
||||
/// Sets the vertical position of this rectangle to the given value,
|
||||
/// clamped to be less than or equal to i32::max_value() / 2.
|
||||
/// clamped to be less than or equal to i32::MAX / 2.
|
||||
pub fn set_y(&mut self, y: i32) {
|
||||
self.raw.y = clamp_position(y);
|
||||
}
|
||||
|
||||
/// Sets the width of this rectangle to the given value,
|
||||
/// clamped to be less than or equal to i32::max_value() / 2.
|
||||
/// clamped to be less than or equal to i32::MAX / 2.
|
||||
///
|
||||
/// `Rect`s must always be non-empty, so a `width` argument of 0 will be
|
||||
/// replaced with 1.
|
||||
@@ -198,7 +198,7 @@ impl Rect {
|
||||
}
|
||||
|
||||
/// Sets the height of this rectangle to the given value,
|
||||
/// clamped to be less than or equal to i32::max_value() / 2.
|
||||
/// clamped to be less than or equal to i32::MAX / 2.
|
||||
///
|
||||
/// `Rect`s must always be non-empty, so a `height` argument of 0 will be
|
||||
/// replaced with 1.
|
||||
@@ -349,13 +349,13 @@ impl Rect {
|
||||
}
|
||||
|
||||
/// Sets the position of the right side of this rectangle to the given
|
||||
/// value, clamped to be less than or equal to i32::max_value() / 2.
|
||||
/// value, clamped to be less than or equal to i32::MAX / 2.
|
||||
pub fn set_right(&mut self, right: i32) {
|
||||
self.raw.x = clamp_position(clamp_position(right) - self.raw.w);
|
||||
}
|
||||
|
||||
/// Sets the position of the bottom side of this rectangle to the given
|
||||
/// value, clamped to be less than or equal to i32::max_value() / 2.
|
||||
/// value, clamped to be less than or equal to i32::MAX / 2.
|
||||
pub fn set_bottom(&mut self, bottom: i32) {
|
||||
self.raw.y = clamp_position(clamp_position(bottom) - self.raw.h);
|
||||
}
|
||||
@@ -391,7 +391,7 @@ impl Rect {
|
||||
if x >= 0 {
|
||||
self.raw.x = max_int_value() as i32;
|
||||
} else {
|
||||
self.raw.x = i32::min_value();
|
||||
self.raw.x = i32::MIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -401,7 +401,7 @@ impl Rect {
|
||||
if y >= 0 {
|
||||
self.raw.y = max_int_value() as i32;
|
||||
} else {
|
||||
self.raw.y = i32::min_value();
|
||||
self.raw.y = i32::MIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -499,10 +499,7 @@ impl Rect {
|
||||
/// If a clipping rectangle is given, only points that are within it will be
|
||||
/// considered.
|
||||
#[doc(alias = "SDL_EnclosePoints")]
|
||||
pub fn from_enclose_points<R: Into<Option<Rect>>>(
|
||||
points: &[Point],
|
||||
clipping_rect: R,
|
||||
) -> Option<Rect>
|
||||
pub fn from_enclose_points<R>(points: &[Point], clipping_rect: R) -> Option<Rect>
|
||||
where
|
||||
R: Into<Option<Rect>>,
|
||||
{
|
||||
@@ -1016,8 +1013,8 @@ mod test {
|
||||
)),
|
||||
Rect::from_enclose_points(
|
||||
&[
|
||||
Point::new(i32::min_value(), i32::min_value()),
|
||||
Point::new(i32::max_value(), i32::max_value())
|
||||
Point::new(i32::MIN, i32::MIN),
|
||||
Point::new(i32::MAX, i32::MAX)
|
||||
],
|
||||
None
|
||||
)
|
||||
@@ -1067,7 +1064,7 @@ mod test {
|
||||
fn clamp_position_min() {
|
||||
assert_eq!(
|
||||
tuple(min_int_value(), min_int_value(), 1, 1),
|
||||
Rect::new(i32::min_value(), i32::min_value(), 1, 1).into()
|
||||
Rect::new(i32::MIN, i32::MIN, 1, 1).into()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1083,7 +1080,7 @@ mod test {
|
||||
fn clamp_i32_max() {
|
||||
assert_eq!(
|
||||
tuple(0, 0, max_int_value(), max_int_value()),
|
||||
Rect::new(0, 0, i32::max_value() as u32, i32::max_value() as u32).into()
|
||||
Rect::new(0, 0, i32::MAX as u32, i32::MAX as u32).into()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ impl fmt::Display for FontError {
|
||||
enum RenderableText<'a> {
|
||||
Utf8(&'a str),
|
||||
Latin1(&'a [u8]),
|
||||
Char(String),
|
||||
Char(char),
|
||||
}
|
||||
impl<'a> RenderableText<'a> {
|
||||
/// Converts the given text to a c-style string if possible.
|
||||
@@ -103,7 +103,9 @@ impl<'a> RenderableText<'a> {
|
||||
Err(err) => Err(FontError::InvalidLatin1Text(err)),
|
||||
Ok(cstring) => Ok(cstring),
|
||||
},
|
||||
RenderableText::Char(ref string) => Ok(CString::new(string.as_bytes()).unwrap()),
|
||||
RenderableText::Char(ch) => {
|
||||
Ok(CString::new(ch.encode_utf8(&mut [0; 4]).as_bytes()).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -336,10 +338,8 @@ impl<'ttf, 'r> Font<'ttf, 'r> {
|
||||
|
||||
/// Starts specifying a rendering of the given UTF-8-encoded character.
|
||||
pub fn render_char(&self, ch: char) -> PartialRendering<'_, 'static> {
|
||||
let mut s = String::new();
|
||||
s.push(ch);
|
||||
PartialRendering {
|
||||
text: RenderableText::Char(s),
|
||||
text: RenderableText::Char(ch),
|
||||
font: self,
|
||||
}
|
||||
}
|
||||
@@ -387,9 +387,7 @@ impl<'ttf, 'r> Font<'ttf, 'r> {
|
||||
/// Returns the width and height of the given text when rendered using this
|
||||
/// font.
|
||||
pub fn size_of_char(&self, ch: char) -> FontResult<(u32, u32)> {
|
||||
let mut s = String::new();
|
||||
s.push(ch);
|
||||
self.size_of(&s)
|
||||
self.size_of(ch.encode_utf8(&mut [0; 4]))
|
||||
}
|
||||
|
||||
/// Returns the font's style flags.
|
||||
@@ -473,29 +471,41 @@ impl<'ttf, 'r> Font<'ttf, 'r> {
|
||||
unsafe { ttf::TTF_FontFaceIsFixedWidth(self.raw) != 0 }
|
||||
}
|
||||
|
||||
/// Returns the family name of the current font face.
|
||||
pub fn face_family_name(&self) -> Option<String> {
|
||||
/// Returns the family name of the current font face without doing any heap allocations.
|
||||
pub fn face_family_name_borrowed(&self) -> Option<&'ttf CStr> {
|
||||
unsafe {
|
||||
// not owns buffer
|
||||
let cname = ttf::TTF_FontFaceFamilyName(self.raw);
|
||||
if cname.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(String::from_utf8_lossy(CStr::from_ptr(cname).to_bytes()).to_string())
|
||||
Some(CStr::from_ptr(cname))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the family name of the current font face.
|
||||
pub fn face_family_name(&self) -> Option<String> {
|
||||
self.face_family_name_borrowed()
|
||||
.map(|cstr| String::from_utf8_lossy(cstr.to_bytes()).into_owned())
|
||||
}
|
||||
|
||||
/// Returns the name of the current font face without doing any heap allocations.
|
||||
pub fn face_style_name_borrowed(&self) -> Option<&'ttf CStr> {
|
||||
unsafe {
|
||||
let cname = ttf::TTF_FontFaceStyleName(self.raw);
|
||||
if cname.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(CStr::from_ptr(cname))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the name of the current font face.
|
||||
pub fn face_style_name(&self) -> Option<String> {
|
||||
unsafe {
|
||||
let cname = ttf::TTF_FontFaceStyleName(self.raw);
|
||||
if cname.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(String::from_utf8_lossy(CStr::from_ptr(cname).to_bytes()).to_string())
|
||||
}
|
||||
}
|
||||
self.face_style_name_borrowed()
|
||||
.map(|cstr| String::from_utf8_lossy(cstr.to_bytes()).into_owned())
|
||||
}
|
||||
|
||||
/// Returns the index of the given character in this font face.
|
||||
|
||||
Reference in New Issue
Block a user