Example showing problem with Bounds2 using Point2::Sub trait.
This commit is contained in:
commit
2b1b9590c0
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
6
Cargo.lock
generated
Normal file
6
Cargo.lock
generated
Normal file
@ -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"
|
||||
|
||||
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "nestedsubtraits"
|
||||
version = "0.1.0"
|
||||
authors = ["Bill Thiede <git@xinu.tv>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
93
src/lib.rs
Normal file
93
src/lib.rs
Normal file
@ -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<T>
|
||||
where
|
||||
T: Number,
|
||||
{
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
impl<T> From<[T; 2]> for Point2<T>
|
||||
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<Float>;
|
||||
/// 2D point type with `isize` members.
|
||||
pub type Point2i = Point2<isize>;
|
||||
|
||||
impl<T> Sub for Point2<T>
|
||||
where
|
||||
T: Number + Sub<Output = T>,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
/// Implement `-` for Point2<T>
|
||||
///
|
||||
/// # 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<T>
|
||||
where
|
||||
T: Number,
|
||||
{
|
||||
pub p_min: Point2<T>,
|
||||
pub p_max: Point2<T>,
|
||||
}
|
||||
|
||||
impl<T> Bounds2<T>
|
||||
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!();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user