Skip to content

Commit

Permalink
refactor: serde derive, from_f64_au
Browse files Browse the repository at this point in the history
Signed-off-by: Florian-Schoenherr <florian.schoenherr99@gmail.com>
  • Loading branch information
Florian-Schoenherr committed Nov 4, 2021
1 parent 60a1bd5 commit d60d142
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -10,7 +10,7 @@ edition = "2021"

[dependencies]
num-traits = { version = "0.2", optional = true }
serde = { version = "1.0", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
default = ["num_traits", "serde_serialization"]
Expand Down
31 changes: 11 additions & 20 deletions src/app_unit.rs
Expand Up @@ -5,7 +5,7 @@
#[cfg(feature = "num_traits")]
use num_traits::Zero;
#[cfg(feature = "serde_serialization")]
use serde::{ser::{Serialize, Serializer}, de::{Deserialize, Deserializer}};
use serde::{Serialize, Deserialize, Deserializer};

use std::{fmt, i32, default::Default, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign}};

Expand All @@ -20,6 +20,7 @@ pub const MAX_AU: Au = Au((1 << 30) - 1);


#[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Eq, Ord, Default)]
#[cfg_attr(feature = "serde_serialization", derive(Serialize))]
/// An App Unit, the fundamental unit of length in Servo. Usually
/// 1/60th of a pixel (see `AU_PER_PX`)
///
Expand All @@ -28,17 +29,16 @@ pub const MAX_AU: Au = Au((1 << 30) - 1);
/// panics and overflows.
pub struct Au(pub i32);

#[cfg(feature = "serde_serialization")]
impl<'de> Deserialize<'de> for Au {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Au, D::Error> {
Ok(Au(i32::deserialize(deserializer)?).clamp())
impl fmt::Debug for Au {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}px", self.to_f64_px())
}
}

#[cfg(feature = "serde_serialization")]
impl Serialize for Au {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.serialize(serializer)
impl<'de> Deserialize<'de> for Au {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Au, D::Error> {
Ok(Au(i32::deserialize(deserializer)?).clamp())
}
}

Expand All @@ -55,12 +55,6 @@ impl Zero for Au {
}
}

impl fmt::Debug for Au {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}px", self.to_f64_px())
}
}

impl Add for Au {
type Output = Au;

Expand Down Expand Up @@ -210,19 +204,16 @@ impl Au {

#[inline]
pub fn from_f64_au(float: f64) -> Self {
// We *must* operate in f64. f32 isn't precise enough
// to handle MAX_AU
Au(float.min(MAX_AU.0 as f64)
.max(MIN_AU.0 as f64)
as i32)
// We *must* operate in f64. f32 isn't precise enough to handle MAX_AU
Au(float.clamp(MIN_AU.0 as f64, MAX_AU.0 as f64) as i32)
}

#[inline]
pub fn from_px(px: i32) -> Au {
Au(px) * AU_PER_PX
}

/// Rounds this app unit down to the pixel towards zero and returns it.
/// Round this app unit down to the pixel towards zero and return it.
#[inline]
pub fn to_px(self) -> i32 {
self.0 / AU_PER_PX
Expand Down

0 comments on commit d60d142

Please sign in to comment.