commit 2b1b9590c003de98864e416f605c2f4f01796f72 Author: Bill Thiede Date: Fri Nov 29 17:01:52 2019 -0800 Example showing problem with Bounds2 using Point2::Sub trait. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..77266f6 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "nestedsubtraits" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e782986 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "nestedsubtraits" +version = "0.1.0" +authors = ["Bill Thiede "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f578dac --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,93 @@ +use std::fmt; +use std::ops::Mul; +use std::ops::Sub; + +type Float = f32; + +pub trait Number +where + Self: std::marker::Sized + Copy + fmt::Display + Mul + Sub, +{ + // Real version has stuff here +} + +impl Number for Float {} +impl Number for isize {} + +/// Generic type for any 2D point. +#[derive(Copy, Clone, Debug, Default, PartialEq)] +pub struct Point2 +where + T: Number, +{ + pub x: T, + pub y: T, +} + +impl From<[T; 2]> for Point2 +where + T: Number, +{ + fn from(xy: [T; 2]) -> Self { + Point2 { x: xy[0], y: xy[1] } + } +} + +/// 2D point type with `Float` members. +pub type Point2f = Point2; +/// 2D point type with `isize` members. +pub type Point2i = Point2; + +impl Sub for Point2 +where + T: Number + Sub, +{ + type Output = Self; + + /// Implement `-` for Point2 + /// + /// # Examples + /// ``` + /// use nestedsubtraits::Point2i; + /// + /// let p1: Point2i = [2, 3].into(); + /// let p2: Point2i = [4, 5].into(); + /// assert_eq!(p2 - p1, [2, 2].into()); + /// + /// use nestedsubtraits::Point2f; + /// + /// let p1: Point2f = [2., 3.].into(); + /// let p2: Point2f = [4., 5.].into(); + /// assert_eq!(p2 - p1, [2., 2.].into()); + /// ``` + fn sub(self, rhs: Self) -> Self::Output { + Point2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} + +/// Generic type for 2D bounding boxes. +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct Bounds2 +where + T: Number, +{ + pub p_min: Point2, + pub p_max: Point2, +} + +impl Bounds2 +where + T: Number, +{ + pub fn area(&self) -> T { + // TODO(glenng) make this compile. + /* + let d = self.p_max - self.p_min; + d.x * d.y + */ + unimplemented!(); + } +}