<commit_before>//! Rectangles and points.
#![allow(const_err)]

use sys::rect as ll;
use std::mem;
use std::ptr;
use std::ops::{Deref, DerefMut, Add, BitAnd, BitOr, Div, Mul, Neg, Sub};
use std::convert::{AsRef, AsMut};

/// The maximal integer value that can be used for rectangles.
///
/// 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
}

/// The minimal integer value that can be used for rectangle positions
/// and points.
///
/// 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
}

fn clamp_size(val: u32) -> u32 {
    if val == 0 {
        1
    } else if val > max_int_value() {
        max_int_value()
    } else {
        val
    }
}

fn clamp_position(val: i32) -> i32 {
    if val > max_int_value() as i32 {
        max_int_value() as i32
    } else if val < min_int_value() {
        min_int_value()
    } else {
        val
    }
}

fn clamped_mul(a: i32, b: i32) -> i32 {
    match a.checked_mul(b) {
        Some(val) => val,
        None => {
            if (a < 0) ^ (b < 0) {
                min_int_value()
            } else {
                max_int_value() as i32
            }
        }
    }
}

/// A rectangle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rect {
    raw: ll::SDL_Rect,
}

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 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.
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rect {
        let raw = ll::SDL_Rect {
            x: clamp_position(x),
            y: clamp_position(y),
            w: clamp_size(width) as i32,
            h: clamp_size(height) as i32,
        };
        Rect { raw: raw }
    }

    /// 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 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.
    pub fn from_center<P>(center: P, width: u32, height: u32)
            -> Rect where P: Into<Point> {
        let raw = ll::SDL_Rect {
            x: 0,
            y: 0,
            w: clamp_size(width) as i32,
            h: clamp_size(height) as i32,
        };
        let mut rect = Rect { raw: raw };
        rect.center_on(center.into());
        rect
    }

    /// The horizontal position of this rectangle.
    pub fn x(&self) -> i32 {
        self.raw.x
    }

    /// The vertical position of this rectangle.
    pub fn y(&self) -> i32 {
        self.raw.y
    }

    /// The width of this rectangle.
    pub fn width(&self) -> u32 {
        self.raw.w as u32
    }

    /// The height of this rectangle.
    pub fn height(&self) -> u32 {
        self.raw.h as u32
    }

    /// Returns the width and height of this rectangle.
    pub fn size(&self) -> (u32, u32) {
        (self.width(), self.height())
    }

    /// Sets the horizontal position of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 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.
    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.
    pub fn set_width(&mut self, width: u32) {
        self.raw.w = clamp_size(width) as i32;
    }

    /// Sets the height of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 2.
    pub fn set_height(&mut self, height: u32) {
        self.raw.h = clamp_size(height) as i32;
    }

    /// Returns the x-position of the left side of this rectangle.
    pub fn left(&self) -> i32 {
        self.raw.x
    }

    /// Returns the x-position of the right side of this rectangle.
    pub fn right(&self) -> i32 {
        self.raw.x + self.raw.w
    }

    /// Returns the y-position of the top side of this rectangle.
    pub fn top(&self) -> i32 {
        self.raw.y
    }
<commit_msg>Clarify Rect docs to indicate that empty Rects aren't allowed<commit_after>//! Rectangles and points.
#![allow(const_err)]

use sys::rect as ll;
use std::mem;
use std::ptr;
use std::ops::{Deref, DerefMut, Add, BitAnd, BitOr, Div, Mul, Neg, Sub};
use std::convert::{AsRef, AsMut};

/// The maximal integer value that can be used for rectangles.
///
/// 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
}

/// The minimal integer value that can be used for rectangle positions
/// and points.
///
/// 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
}

fn clamp_size(val: u32) -> u32 {
    if val == 0 {
        1
    } else if val > max_int_value() {
        max_int_value()
    } else {
        val
    }
}

fn clamp_position(val: i32) -> i32 {
    if val > max_int_value() as i32 {
        max_int_value() as i32
    } else if val < min_int_value() {
        min_int_value()
    } else {
        val
    }
}

fn clamped_mul(a: i32, b: i32) -> i32 {
    match a.checked_mul(b) {
        Some(val) => val,
        None => {
            if (a < 0) ^ (b < 0) {
                min_int_value()
            } else {
                max_int_value() as i32
            }
        }
    }
}

/// A (non-empty) rectangle.
///
/// The width and height of a `Rect` must always be strictly positive (never
/// zero).  In cases where empty rects may need to represented, it is
/// recommended to use `Option<Rect>`, with `None` representing an empty
/// rectangle (see, for example, the output of the
/// [`intersection`](#method.intersection) method).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rect {
    raw: ll::SDL_Rect,
}

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
    /// 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.
    ///
    /// `Rect`s must always be non-empty, so a `width` and/or `height` argument
    /// of 0 will be replaced with 1.
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rect {
        let raw = ll::SDL_Rect {
            x: clamp_position(x),
            y: clamp_position(y),
            w: clamp_size(width) as i32,
            h: clamp_size(height) as i32,
        };
        Rect { raw: raw }
    }

    /// 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
    /// 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.
    ///
    /// `Rect`s must always be non-empty, so a `width` and/or `height` argument
    /// of 0 will be replaced with 1.
    pub fn from_center<P>(center: P, width: u32, height: u32)
            -> Rect where P: Into<Point> {
        let raw = ll::SDL_Rect {
            x: 0,
            y: 0,
            w: clamp_size(width) as i32,
            h: clamp_size(height) as i32,
        };
        let mut rect = Rect { raw: raw };
        rect.center_on(center.into());
        rect
    }

    /// The horizontal position of this rectangle.
    pub fn x(&self) -> i32 {
        self.raw.x
    }

    /// The vertical position of this rectangle.
    pub fn y(&self) -> i32 {
        self.raw.y
    }

    /// The width of this rectangle.
    pub fn width(&self) -> u32 {
        self.raw.w as u32
    }

    /// The height of this rectangle.
    pub fn height(&self) -> u32 {
        self.raw.h as u32
    }

    /// Returns the width and height of this rectangle.
    pub fn size(&self) -> (u32, u32) {
        (self.width(), self.height())
    }

    /// Sets the horizontal position of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 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.
    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.
    ///
    /// `Rect`s must always be non-empty, so a `width` argument of 0 will be
    /// replaced with 1.
    pub fn set_width(&mut self, width: u32) {
        self.raw.w = clamp_size(width) as i32;
    }

    /// Sets the height of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 2.
    ///
    /// `Rect`s must always be non-empty, so a `height` argument of 0 will be
    /// replaced with 1.
    pub fn set_height(&mut self, height: u32) {
        self.raw.h = clamp_size(height) as i32;
    }

    /// Returns the x-position of the left side of this rectangle.
    pub fn left(&self) -> i32 {
        self.raw.x
    }

    /// Returns the x-position of the right side of this rectangle.
    pub fn right(&self) -> i32 {
        self.raw.x + self.raw.w
    }

    /// Returns the y-position of the top side of this rectangle.
    pub fn top(&self) -> i32 {
        self.raw.y
    }

{'fraction_non_alphanumeric': 0.11917293233082707, 'fraction_numerical': 0.02556390977443609, 'mean_word_length': 1.9723463687150837, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6226722', 'n_tokens_mistral': 3549, 'n_tokens_neox': 3323, 'n_words': 1308}