Skip to content

Commit

Permalink
Merge pull request #1 from adamnemecek/master
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
mrDIMAS committed Sep 20, 2020
2 parents 3618142 + a10d54d commit e1675a2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 85 deletions.
16 changes: 8 additions & 8 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ impl<T> Rect<T>
where
T: PartialOrd + Default + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Copy,
{
pub fn new(x: T, y: T, w: T, h: T) -> Rect<T> {
Rect { x, y, w, h }
pub fn new(x: T, y: T, w: T, h: T) -> Self {
Self { x, y, w, h }
}

pub fn default() -> Rect<T> {
Rect {
pub fn default() -> Self {
Self {
x: T::default(),
y: T::default(),
w: T::default(),
Expand All @@ -55,8 +55,8 @@ where
}

#[must_use = "this method creates new instance of rect"]
pub fn inflate(&self, dw: T, dh: T) -> Rect<T> {
Rect {
pub fn inflate(&self, dw: T, dh: T) -> Self {
Self {
x: self.x - dw,
y: self.y - dh,
w: self.w + dw + dw,
Expand All @@ -65,8 +65,8 @@ where
}

#[must_use = "this method creates new instance of rect"]
pub fn deflate(&self, dw: T, dh: T) -> Rect<T> {
Rect {
pub fn deflate(&self, dw: T, dh: T) -> Self {
Self {
x: self.x + dw,
y: self.y + dh,
w: self.w - (dw + dw),
Expand Down
27 changes: 18 additions & 9 deletions src/math/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ impl Default for Vec2 {
}

impl Vec2 {
pub const ZERO: Self = Vec2 { x: 0.0, y: 0.0 };
pub const UNIT: Self = Vec2 { x: 1.0, y: 1.0 };
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
pub const UNIT: Self = Self { x: 1.0, y: 1.0 };

pub const X: Self = Self {
x: 1.0,
y: 0.0,
};
pub const Y: Self = Self {
x: 0.0,
y: 1.0,
};

fn validate(self) {
debug_assert!(!self.x.is_nan());
Expand All @@ -48,7 +57,7 @@ impl Vec2 {

#[inline]
pub const fn new(x: f32, y: f32) -> Self {
Vec2 { x, y }
Self { x, y }
}

#[inline]
Expand All @@ -67,16 +76,16 @@ impl Vec2 {
}

#[inline]
pub fn perpendicular(self) -> Vec2 {
Vec2 {
pub fn perpendicular(self) -> Self {
Self {
x: self.y,
y: -self.x,
}
}

#[inline]
pub fn scale(self, scalar: f32) -> Vec2 {
Vec2 {
pub fn scale(self, scalar: f32) -> Self {
Self {
x: self.x * scalar,
y: self.y * scalar,
}
Expand All @@ -99,11 +108,11 @@ impl Vec2 {
}

#[inline]
pub fn normalized(self) -> Option<Vec2> {
pub fn normalized(self) -> Option<Self> {
let len = self.len();
if len >= std::f32::EPSILON {
let inv_len = 1.0 / len;
return Some(Vec2 {
return Some(Self {
x: self.x * inv_len,
y: self.y * inv_len,
});
Expand Down
43 changes: 17 additions & 26 deletions src/math/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,26 @@ impl Vec3 {
y: 1.0,
z: 1.0,
};
pub const RIGHT: Self = Self {
pub const X: Self = Self {
x: 1.0,
y: 0.0,
z: 0.0,
};
pub const UP: Self = Self {
pub const Y: Self = Self {
x: 0.0,
y: 1.0,
z: 0.0,
};
pub const LOOK: Self = Self {
pub const Z: Self = Self {
x: 0.0,
y: 0.0,
z: 1.0,
};

pub const RIGHT: Self = Self::X;
pub const UP: Self = Self::Y;
pub const LOOK: Self = Self::Z;

fn validate(&self) {
debug_assert!(!self.x.is_nan());
debug_assert!(!self.y.is_nan());
Expand All @@ -76,7 +80,7 @@ impl Vec3 {

#[inline]
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Vec3 { x, y, z }
Self { x, y, z }
}

pub const fn xy(&self) -> Vec2 {
Expand Down Expand Up @@ -105,7 +109,7 @@ impl Vec3 {

#[inline]
pub fn scale(&self, scalar: f32) -> Self {
Vec3 {
Self {
x: self.x * scalar,
y: self.y * scalar,
z: self.z * scalar,
Expand All @@ -114,7 +118,7 @@ impl Vec3 {

#[inline]
pub fn sqr_len(&self) -> f32 {
self.x * self.x + self.y * self.y + self.z * self.z
self.dot(self)
}

#[inline]
Expand Down Expand Up @@ -206,24 +210,12 @@ impl Vec3 {

#[inline]
pub fn min_value(&self) -> f32 {
if self.x < self.y && self.x < self.z {
self.x
} else if self.y < self.z {
self.y
} else {
self.z
}
self.x.min(self.y).min(self.z)
}

#[inline]
pub fn max_value(&self) -> f32 {
if self.x > self.y && self.x > self.z {
self.x
} else if self.y > self.z {
self.y
} else {
self.z
}
self.x.max(self.y).max(self.z)
}

pub fn follow(&mut self, other: &Self, fraction: f32) {
Expand Down Expand Up @@ -308,12 +300,11 @@ impl ops::Index<usize> for Vec3 {
type Output = f32;

fn index(&self, index: usize) -> &Self::Output {
if index == 0 {
&self.x
} else if index == 1 {
&self.y
} else {
&self.z
match index {
0 => &self.x,
1 => &self.y,
2 => &self.z,
_ => panic!("Invalid index {:?} for Vec3", index),
}
}
}
8 changes: 4 additions & 4 deletions src/rectpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ struct RectPackNode<T> {
}

impl<T> RectPackNode<T> {
fn new(bounds: Rect<T>) -> RectPackNode<T> {
RectPackNode {
fn new(bounds: Rect<T>) -> Self {
Self {
bounds,
filled: false,
split: false,
Expand Down Expand Up @@ -45,15 +45,15 @@ where
/// then calculate total area of your triangles by sum of width*height and then take square
/// root out of area. You'll get side length of a square which can be used as width and height
/// parameters.
pub fn new(w: T, h: T) -> RectPacker<T> {
pub fn new(w: T, h: T) -> Self {
let mut nodes = Pool::new();
let root = nodes.spawn(RectPackNode::new(Rect::new(
Default::default(),
Default::default(),
w,
h,
)));
RectPacker { nodes, root }
Self { nodes, root }
}

/// Tries to find free place to put rectangle with given size. Returns None if there insufficient
Expand Down
76 changes: 38 additions & 38 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,44 @@ pub enum FieldKind {
impl FieldKind {
fn as_string(&self) -> String {
match self {
FieldKind::Bool(data) => format!("<bool = {}>, ", data),
FieldKind::U8(data) => format!("<u8 = {}>, ", data),
FieldKind::I8(data) => format!("<i8 = {}>, ", data),
FieldKind::U16(data) => format!("<u16 = {}>, ", data),
FieldKind::I16(data) => format!("<i16 = {}>, ", data),
FieldKind::U32(data) => format!("<u32 = {}>, ", data),
FieldKind::I32(data) => format!("<i32 = {}>, ", data),
FieldKind::U64(data) => format!("<u64 = {}>, ", data),
FieldKind::I64(data) => format!("<i64 = {}>, ", data),
FieldKind::F32(data) => format!("<f32 = {}>, ", data),
FieldKind::F64(data) => format!("<f64 = {}>, ", data),
FieldKind::Vec3(data) => format!("<vec3 = {}; {}; {}>, ", data.x, data.y, data.z),
FieldKind::Quat(data) => {
Self::Bool(data) => format!("<bool = {}>, ", data),
Self::U8(data) => format!("<u8 = {}>, ", data),
Self::I8(data) => format!("<i8 = {}>, ", data),
Self::U16(data) => format!("<u16 = {}>, ", data),
Self::I16(data) => format!("<i16 = {}>, ", data),
Self::U32(data) => format!("<u32 = {}>, ", data),
Self::I32(data) => format!("<i32 = {}>, ", data),
Self::U64(data) => format!("<u64 = {}>, ", data),
Self::I64(data) => format!("<i64 = {}>, ", data),
Self::F32(data) => format!("<f32 = {}>, ", data),
Self::F64(data) => format!("<f64 = {}>, ", data),
Self::Vec3(data) => format!("<vec3 = {}; {}; {}>, ", data.x, data.y, data.z),
Self::Quat(data) => {
format!("<quat = {}; {}; {}; {}>, ", data.x, data.y, data.z, data.w)
}
FieldKind::Mat4(data) => {
Self::Mat4(data) => {
let mut out = String::from("<mat4 = ");
for f in &data.f {
out += format!("{}; ", f).as_str();
}
out
}
FieldKind::Data(data) => {
Self::Data(data) => {
let out = match String::from_utf8(data.clone()) {
Ok(s) => s,
Err(_) => base64::encode(data),
};
format!("<data = {}>, ", out)
}
FieldKind::Mat3(data) => {
Self::Mat3(data) => {
let mut out = String::from("<mat3 = ");
for f in &data.f {
out += format!("{}; ", f).as_str();
}
out
}
FieldKind::Vec2(data) => format!("<vec2 = {}; {}>, ", data.x, data.y),
FieldKind::Vec4(data) => {
Self::Vec2(data) => format!("<vec2 = {}; {}>, ", data.x, data.y),
Self::Vec4(data) => {
format!("<vec4 = {}; {}; {}; {}>, ", data.x, data.y, data.z, data.w)
}
}
Expand Down Expand Up @@ -210,51 +210,51 @@ pub enum VisitError {
impl Display for VisitError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
VisitError::Io(io) => write!(f, "io error: {}", io),
VisitError::UnknownFieldType(type_index) => {
Self::Io(io) => write!(f, "io error: {}", io),
Self::UnknownFieldType(type_index) => {
write!(f, "unknown field type {}", type_index)
}
VisitError::FieldDoesNotExist(name) => write!(f, "field does not exists {}", name),
VisitError::FieldAlreadyExists(name) => write!(f, "field already exists {}", name),
VisitError::RegionAlreadyExists(name) => write!(f, "region already exists {}", name),
VisitError::InvalidCurrentNode => write!(f, "invalid current node"),
VisitError::FieldTypeDoesNotMatch => write!(f, "field type does not match"),
VisitError::RegionDoesNotExist(name) => write!(f, "region does not exists {}", name),
VisitError::NoActiveNode => write!(f, "no active node"),
VisitError::NotSupportedFormat => write!(f, "not supported format"),
VisitError::InvalidName => write!(f, "invalid name"),
VisitError::TypeMismatch => write!(f, "type mismatch"),
VisitError::RefCellAlreadyMutableBorrowed => {
Self::FieldDoesNotExist(name) => write!(f, "field does not exists {}", name),
Self::FieldAlreadyExists(name) => write!(f, "field already exists {}", name),
Self::RegionAlreadyExists(name) => write!(f, "region already exists {}", name),
Self::InvalidCurrentNode => write!(f, "invalid current node"),
Self::FieldTypeDoesNotMatch => write!(f, "field type does not match"),
Self::RegionDoesNotExist(name) => write!(f, "region does not exists {}", name),
Self::NoActiveNode => write!(f, "no active node"),
Self::NotSupportedFormat => write!(f, "not supported format"),
Self::InvalidName => write!(f, "invalid name"),
Self::TypeMismatch => write!(f, "type mismatch"),
Self::RefCellAlreadyMutableBorrowed => {
write!(f, "ref cell already mutable borrowed")
}
VisitError::User(msg) => write!(f, "user defined error: {}", msg),
VisitError::UnexpectedRcNullIndex => write!(f, "unexpected rc null index"),
VisitError::PoisonedMutex => write!(f, "attempt to lock poisoned mutex"),
Self::User(msg) => write!(f, "user defined error: {}", msg),
Self::UnexpectedRcNullIndex => write!(f, "unexpected rc null index"),
Self::PoisonedMutex => write!(f, "attempt to lock poisoned mutex"),
}
}
}

impl<'a, T> From<std::sync::PoisonError<std::sync::MutexGuard<'a, T>>> for VisitError {
fn from(_: std::sync::PoisonError<std::sync::MutexGuard<'a, T>>) -> Self {
VisitError::PoisonedMutex
Self::PoisonedMutex
}
}

impl From<std::io::Error> for VisitError {
fn from(io_err: std::io::Error) -> Self {
VisitError::Io(io_err)
Self::Io(io_err)
}
}

impl From<FromUtf8Error> for VisitError {
fn from(_: FromUtf8Error) -> Self {
VisitError::InvalidName
Self::InvalidName
}
}

impl From<String> for VisitError {
fn from(s: String) -> Self {
VisitError::User(s)
Self::User(s)
}
}

Expand Down

0 comments on commit e1675a2

Please sign in to comment.