Created
January 9, 2019 03:09
-
-
Save etscrivner/1de540fe20ea87e911557adccc63ca0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, Copy, Clone, PartialEq)] | |
pub struct Point<T> { | |
pub a : T, | |
pub b : T, | |
pub x : Option<T>, | |
pub y : Option<T> | |
} | |
impl<T> Point<T> { | |
pub fn infinity(a: T, b: T) -> Self { | |
Self { x: None, y: None, a: a, b: b } | |
} | |
fn is_identity(&self) -> bool { | |
self.x.is_none() || self.y.is_none() | |
} | |
} | |
trait PointOps<T> { | |
fn is_valid(&self) -> bool; | |
fn slope(&self, other: &Self) -> T; | |
fn tangent_slope(&self) -> T; | |
} | |
impl PointOps<f64> for Point<f64> { | |
fn is_valid(&self) -> bool { | |
self.y.and_then(|v| Some(v.powf(2.0))) == | |
self.x.and_then(|v| Some(v.powf(3.0) + self.a * v + self.b)) | |
} | |
fn slope(&self, other: &Self) -> f64 { | |
(other.y.unwrap() - self.y.unwrap()) / (other.x.unwrap() - self.x.unwrap()) | |
} | |
fn tangent_slope(&self) -> f64 { | |
(3.0 * self.x.unwrap() * self.x.unwrap() + self.a) / (2.0 * self.y.unwrap()) | |
} | |
} | |
fn main() { | |
for (x, y) in vec![(2.0, 4.0), (-1.0, -1.0), (18.0, 77.0), (5.0, 7.0)] { | |
let pt = Point::<f64> { x: Some(x), y: Some(y), a: a, b: b }; | |
if pt.is_valid() { | |
println!("({}, {}) OK.", x, y) | |
} else { | |
println!("({}, {}) NOT OK.", x, y); | |
} | |
} | |
} | |
/* | |
error[E0599]: no method named `is_valid` found for type `ec::Point<f64>` in the current scope | |
--> src/main.rs:29:15 | |
| | |
29 | if pt.is_valid() { | |
| ^^^^^^^^ | |
| | |
::: src/ec.rs:7:1 | |
| | |
7 | pub struct Point<T> { | |
| ------------------- method `is_valid` not found for this | |
| | |
= help: items from traits can only be used if the trait is implemented and in scope | |
= note: the following trait defines an item `is_valid`, perhaps you need to implement it: | |
candidate #1: `PointOps` | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment