Add lots of Eq impls

This commit is contained in:
Laurenz 2021-07-08 23:16:02 +02:00
parent fd0b89a1d8
commit 02b586cc36
14 changed files with 51 additions and 45 deletions

View File

@ -8,7 +8,7 @@ use crate::layout::Paint;
use crate::paper::{Paper, PaperClass, PAPER_A4}; use crate::paper::{Paper, PaperClass, PAPER_A4};
/// The execution state. /// The execution state.
#[derive(Default, Debug, Clone, PartialEq, Hash)] #[derive(Default, Debug, Clone, Eq, PartialEq, Hash)]
pub struct State { pub struct State {
/// The current language-related settings. /// The current language-related settings.
pub lang: LangState, pub lang: LangState,
@ -30,7 +30,7 @@ impl State {
} }
/// Defines language properties. /// Defines language properties.
#[derive(Debug, Copy, Clone, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LangState { pub struct LangState {
/// The direction for text and other inline objects. /// The direction for text and other inline objects.
pub dir: Dir, pub dir: Dir,
@ -43,7 +43,7 @@ impl Default for LangState {
} }
/// Defines page properties. /// Defines page properties.
#[derive(Debug, Copy, Clone, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct PageState { pub struct PageState {
/// The class of this page. /// The class of this page.
pub class: PaperClass, pub class: PaperClass,
@ -83,7 +83,7 @@ impl Default for PageState {
} }
/// Defines paragraph properties. /// Defines paragraph properties.
#[derive(Debug, Copy, Clone, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ParState { pub struct ParState {
/// The spacing between paragraphs (dependent on scaled font size). /// The spacing between paragraphs (dependent on scaled font size).
pub spacing: Linear, pub spacing: Linear,
@ -105,7 +105,7 @@ impl Default for ParState {
} }
/// Defines font properties. /// Defines font properties.
#[derive(Debug, Clone, PartialEq, Hash)] #[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct FontState { pub struct FontState {
/// A list of font families with generic class definitions. /// A list of font families with generic class definitions.
pub families: Rc<FamilyList>, pub families: Rc<FamilyList>,
@ -163,7 +163,7 @@ impl Default for FontState {
} }
/// Describes a line that could be positioned over, under or on top of text. /// Describes a line that could be positioned over, under or on top of text.
#[derive(Debug, Copy, Clone, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LineState { pub struct LineState {
/// Stroke color of the line. /// Stroke color of the line.
/// ///

View File

@ -4,6 +4,7 @@ use std::collections::HashMap;
use std::fmt::{self, Debug, Display, Formatter}; use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Add; use std::ops::Add;
use decorum::N64;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::geom::Length; use crate::geom::Length;
@ -119,7 +120,7 @@ impl Face {
VerticalFontMetric::Ascender => self.ascender, VerticalFontMetric::Ascender => self.ascender,
VerticalFontMetric::CapHeight => self.cap_height, VerticalFontMetric::CapHeight => self.cap_height,
VerticalFontMetric::XHeight => self.x_height, VerticalFontMetric::XHeight => self.x_height,
VerticalFontMetric::Baseline => Em::ZERO, VerticalFontMetric::Baseline => Em::zero(),
VerticalFontMetric::Descender => self.descender, VerticalFontMetric::Descender => self.descender,
} }
} }
@ -161,31 +162,33 @@ impl Display for VerticalFontMetric {
/// A length in em units. /// A length in em units.
/// ///
/// `1em` is the same as the font size. /// `1em` is the same as the font size.
#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd)] #[derive(Default, Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
pub struct Em(f64); pub struct Em(N64);
impl Em { impl Em {
/// The zero length. /// The zero length.
pub const ZERO: Self = Self(0.0); pub fn zero() -> Self {
Self(N64::from(0.0))
}
/// Create an em length. /// Create an em length.
pub fn new(em: f64) -> Self { pub fn new(em: f64) -> Self {
Self(em) Self(N64::from(em))
} }
/// Convert units to an em length at the given units per em. /// Convert units to an em length at the given units per em.
pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self { pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self {
Self(units.into() / units_per_em) Self(N64::from(units.into() / units_per_em))
} }
/// The number of em units. /// The number of em units.
pub fn get(self) -> f64 { pub fn get(self) -> f64 {
self.0 self.0.into()
} }
/// Convert to a length at the given font size. /// Convert to a length at the given font size.
pub fn to_length(self, font_size: Length) -> Length { pub fn to_length(self, font_size: Length) -> Length {
self.0 * font_size self.get() * font_size
} }
} }
@ -317,7 +320,7 @@ impl FaceId {
} }
/// Properties of a single font face. /// Properties of a single font face.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct FaceInfo { pub struct FaceInfo {
/// The typographic font family this face is part of. /// The typographic font family this face is part of.
pub family: String, pub family: String,
@ -330,7 +333,8 @@ pub struct FaceInfo {
} }
/// Properties that distinguish a face from other faces in the same family. /// Properties that distinguish a face from other faces in the same family.
#[derive(Default, Debug, Copy, Clone, PartialEq, Hash, Serialize, Deserialize)] #[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[derive(Serialize, Deserialize)]
pub struct FontVariant { pub struct FontVariant {
/// The style of the face (normal / italic / oblique). /// The style of the face (normal / italic / oblique).
pub style: FontStyle, pub style: FontStyle,

View File

@ -1,7 +1,7 @@
use super::*; use super::*;
/// A node that places a rectangular filled background behind its child. /// A node that places a rectangular filled background behind its child.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub struct BackgroundNode { pub struct BackgroundNode {
/// The kind of shape to use as a background. /// The kind of shape to use as a background.

View File

@ -1,7 +1,7 @@
use super::*; use super::*;
/// A node that can fix its child's width and height. /// A node that can fix its child's width and height.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub struct FixedNode { pub struct FixedNode {
/// The fixed width, if any. /// The fixed width, if any.

View File

@ -9,7 +9,7 @@ use crate::geom::{Length, Path, Point, Size};
use crate::image::ImageId; use crate::image::ImageId;
/// A finished layout with elements at fixed positions. /// A finished layout with elements at fixed positions.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Frame { pub struct Frame {
/// The size of the frame. /// The size of the frame.
pub size: Size, pub size: Size,
@ -99,14 +99,14 @@ impl Frame {
/// A frame can contain multiple children: elements or other frames, complete /// A frame can contain multiple children: elements or other frames, complete
/// with their children. /// with their children.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
enum Child { enum Child {
Element(Element), Element(Element),
Frame(Rc<Frame>), Frame(Rc<Frame>),
} }
/// The building block frames are composed of. /// The building block frames are composed of.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Element { pub enum Element {
/// Shaped text. /// Shaped text.
Text(Text), Text(Text),
@ -118,7 +118,7 @@ pub enum Element {
} }
/// A run of shaped text. /// A run of shaped text.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Text { pub struct Text {
/// The font face the glyphs are contained in. /// The font face the glyphs are contained in.
pub face_id: FaceId, pub face_id: FaceId,
@ -131,7 +131,7 @@ pub struct Text {
} }
/// A glyph in a run of shaped text. /// A glyph in a run of shaped text.
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Glyph { pub struct Glyph {
/// The glyph's index in the face. /// The glyph's index in the face.
pub id: u16, pub id: u16,
@ -155,7 +155,7 @@ impl Text {
} }
/// A geometric shape. /// A geometric shape.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Geometry { pub enum Geometry {
/// A filled rectangle with its origin in the topleft corner. /// A filled rectangle with its origin in the topleft corner.
Rect(Size), Rect(Size),

View File

@ -1,7 +1,7 @@
use super::*; use super::*;
/// A node that arranges its children in a grid. /// A node that arranges its children in a grid.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub struct GridNode { pub struct GridNode {
/// The `main` and `cross` directions of this grid. /// The `main` and `cross` directions of this grid.
@ -18,7 +18,7 @@ pub struct GridNode {
} }
/// Defines how to size a grid cell along an axis. /// Defines how to size a grid cell along an axis.
#[derive(Debug, Copy, Clone, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum TrackSizing { pub enum TrackSizing {
/// Fit the cell to its contents. /// Fit the cell to its contents.
Auto, Auto,

View File

@ -4,7 +4,7 @@ use crate::image::ImageId;
use ::image::GenericImageView; use ::image::GenericImageView;
/// An image node. /// An image node.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub struct ImageNode { pub struct ImageNode {
/// The id of the image file. /// The id of the image file.

View File

@ -168,7 +168,7 @@ impl FramesEntry {
} }
/// Describe regions that match them. /// Describe regions that match them.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Constraints { pub struct Constraints {
/// The minimum available length in the region. /// The minimum available length in the region.
pub min: Spec<Option<Length>>, pub min: Spec<Option<Length>>,
@ -253,7 +253,7 @@ impl Constraints {
/// Carries an item that only applies to certain regions and the constraints /// Carries an item that only applies to certain regions and the constraints
/// that describe these regions. /// that describe these regions.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Constrained<T> { pub struct Constrained<T> {
pub item: T, pub item: T,
pub constraints: Constraints, pub constraints: Constraints,

View File

@ -51,7 +51,7 @@ pub fn layout(
} }
/// A tree of layout nodes. /// A tree of layout nodes.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct LayoutTree { pub struct LayoutTree {
/// Runs of pages with the same properties. /// Runs of pages with the same properties.
pub runs: Vec<PageRun>, pub runs: Vec<PageRun>,
@ -65,7 +65,7 @@ impl LayoutTree {
} }
/// A run of pages that all have the same properties. /// A run of pages that all have the same properties.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct PageRun { pub struct PageRun {
/// The size of each page. /// The size of each page.
pub size: Size, pub size: Size,
@ -98,7 +98,7 @@ impl AnyNode {
#[cfg(feature = "layout-cache")] #[cfg(feature = "layout-cache")]
pub fn new<T>(node: T) -> Self pub fn new<T>(node: T) -> Self
where where
T: Layout + Debug + Clone + PartialEq + Hash + 'static, T: Layout + Debug + Clone + Eq + PartialEq + Hash + 'static,
{ {
let hash = { let hash = {
let mut state = FxHasher64::default(); let mut state = FxHasher64::default();
@ -153,6 +153,8 @@ impl Clone for AnyNode {
} }
} }
impl Eq for AnyNode {}
impl PartialEq for AnyNode { impl PartialEq for AnyNode {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.node.dyn_eq(other.node.as_ref()) self.node.dyn_eq(other.node.as_ref())
@ -180,7 +182,7 @@ trait Bounds: Layout + Debug + 'static {
impl<T> Bounds for T impl<T> Bounds for T
where where
T: Layout + Debug + PartialEq + Clone + 'static, T: Layout + Debug + Eq + PartialEq + Clone + 'static,
{ {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
@ -221,7 +223,7 @@ pub struct LayoutContext<'a> {
} }
/// A sequence of regions to layout into. /// A sequence of regions to layout into.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct Regions { pub struct Regions {
/// The remaining size of the current region. /// The remaining size of the current region.
pub current: Size, pub current: Size,

View File

@ -1,7 +1,7 @@
use super::*; use super::*;
/// A node that adds padding to its child. /// A node that adds padding to its child.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub struct PadNode { pub struct PadNode {
/// The amount of padding. /// The amount of padding.

View File

@ -11,7 +11,7 @@ use crate::util::{RangeExt, SliceExt};
type Range = std::ops::Range<usize>; type Range = std::ops::Range<usize>;
/// A node that arranges its children into a paragraph. /// A node that arranges its children into a paragraph.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub struct ParNode { pub struct ParNode {
/// The inline direction of this paragraph. /// The inline direction of this paragraph.
@ -23,7 +23,7 @@ pub struct ParNode {
} }
/// A child of a paragraph node. /// A child of a paragraph node.
#[derive(Clone, PartialEq)] #[derive(Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub enum ParChild { pub enum ParChild {
/// Spacing between other nodes. /// Spacing between other nodes.

View File

@ -3,7 +3,7 @@ use decorum::N64;
use super::*; use super::*;
/// A node that stacks its children. /// A node that stacks its children.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub struct StackNode { pub struct StackNode {
/// The `main` and `cross` directions of this stack. /// The `main` and `cross` directions of this stack.
@ -20,7 +20,7 @@ pub struct StackNode {
} }
/// A child of a stack node. /// A child of a stack node.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub enum StackChild { pub enum StackChild {
/// Spacing between other nodes. /// Spacing between other nodes.

View File

@ -8,9 +8,9 @@ pub struct Paper {
/// The broad class this paper belongs to. /// The broad class this paper belongs to.
pub class: PaperClass, pub class: PaperClass,
/// The width of the paper in millimeters. /// The width of the paper in millimeters.
pub width: f64, width: f64,
/// The height of the paper in millimeters. /// The height of the paper in millimeters.
pub height: f64, height: f64,
} }
impl Paper { impl Paper {

View File

@ -157,7 +157,7 @@ pub enum Token<'s> {
} }
/// A quoted string token: `"..."`. /// A quoted string token: `"..."`.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct StrToken<'s> { pub struct StrToken<'s> {
/// The string inside the quotes. /// The string inside the quotes.
/// ///
@ -170,7 +170,7 @@ pub struct StrToken<'s> {
} }
/// A raw block token: `` `...` ``. /// A raw block token: `` `...` ``.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct RawToken<'s> { pub struct RawToken<'s> {
/// The raw text between the backticks. /// The raw text between the backticks.
pub text: &'s str, pub text: &'s str,
@ -181,7 +181,7 @@ pub struct RawToken<'s> {
} }
/// A math formula token: `$2pi + x$` or `$[f'(x) = x^2]$`. /// A math formula token: `$2pi + x$` or `$[f'(x) = x^2]$`.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MathToken<'s> { pub struct MathToken<'s> {
/// The formula between the dollars. /// The formula between the dollars.
pub formula: &'s str, pub formula: &'s str,
@ -193,7 +193,7 @@ pub struct MathToken<'s> {
} }
/// A unicode escape sequence token: `\u{1F5FA}`. /// A unicode escape sequence token: `\u{1F5FA}`.
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct UnicodeEscapeToken<'s> { pub struct UnicodeEscapeToken<'s> {
/// The escape sequence between the braces. /// The escape sequence between the braces.
pub sequence: &'s str, pub sequence: &'s str,