diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/traits/inheritance/self-in-supertype.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/test/ui/traits/inheritance/self-in-supertype.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/test/ui/traits/inheritance/self-in-supertype.rs b/src/test/ui/traits/inheritance/self-in-supertype.rs new file mode 100644 index 000000000..e8a2bd791 --- /dev/null +++ b/src/test/ui/traits/inheritance/self-in-supertype.rs @@ -0,0 +1,62 @@ +// run-pass +// Test for issue #4183: use of Self in supertraits. + +pub static FUZZY_EPSILON: f64 = 0.1; + +pub trait FuzzyEq<Eps> { + fn fuzzy_eq(&self, other: &Self) -> bool; + fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool; +} + +trait Float: Sized+FuzzyEq<Self> { + fn two_pi() -> Self; +} + +impl FuzzyEq<f32> for f32 { + fn fuzzy_eq(&self, other: &f32) -> bool { + self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32)) + } + + fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool { + (*self - *other).abs() < *epsilon + } +} + +impl Float for f32 { + fn two_pi() -> f32 { 6.28318530717958647692528676655900576_f32 } +} + +impl FuzzyEq<f64> for f64 { + fn fuzzy_eq(&self, other: &f64) -> bool { + self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64)) + } + + fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool { + (*self - *other).abs() < *epsilon + } +} + +impl Float for f64 { + fn two_pi() -> f64 { 6.28318530717958647692528676655900576_f64 } +} + +fn compare<F:Float>(f1: F) -> bool { + let f2 = Float::two_pi(); + f1.fuzzy_eq(&f2) +} + +pub fn main() { + assert!(compare::<f32>(6.28318530717958647692528676655900576)); + assert!(compare::<f32>(6.29)); + assert!(compare::<f32>(6.3)); + assert!(compare::<f32>(6.19)); + assert!(!compare::<f32>(7.28318530717958647692528676655900576)); + assert!(!compare::<f32>(6.18)); + + assert!(compare::<f64>(6.28318530717958647692528676655900576)); + assert!(compare::<f64>(6.29)); + assert!(compare::<f64>(6.3)); + assert!(compare::<f64>(6.19)); + assert!(!compare::<f64>(7.28318530717958647692528676655900576)); + assert!(!compare::<f64>(6.18)); +} |