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};
/// The execution state.
#[derive(Default, Debug, Clone, PartialEq, Hash)]
#[derive(Default, Debug, Clone, Eq, PartialEq, Hash)]
pub struct State {
/// The current language-related settings.
pub lang: LangState,
@ -30,7 +30,7 @@ impl State {
}
/// Defines language properties.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LangState {
/// The direction for text and other inline objects.
pub dir: Dir,
@ -43,7 +43,7 @@ impl Default for LangState {
}
/// Defines page properties.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct PageState {
/// The class of this page.
pub class: PaperClass,
@ -83,7 +83,7 @@ impl Default for PageState {
}
/// Defines paragraph properties.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ParState {
/// The spacing between paragraphs (dependent on scaled font size).
pub spacing: Linear,
@ -105,7 +105,7 @@ impl Default for ParState {
}
/// Defines font properties.
#[derive(Debug, Clone, PartialEq, Hash)]
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct FontState {
/// A list of font families with generic class definitions.
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.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LineState {
/// Stroke color of the line.
///

View File

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

View File

@ -1,7 +1,7 @@
use super::*;
/// 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))]
pub struct BackgroundNode {
/// The kind of shape to use as a background.

View File

@ -1,7 +1,7 @@
use super::*;
/// 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))]
pub struct FixedNode {
/// The fixed width, if any.

View File

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

View File

@ -1,7 +1,7 @@
use super::*;
/// 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))]
pub struct GridNode {
/// 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.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum TrackSizing {
/// Fit the cell to its contents.
Auto,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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