diff options
| author | bors <bors@rust-lang.org> | 2013-04-09 17:12:58 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-04-09 17:12:58 -0700 |
| commit | 92e265cdea0c528ccca4ccdd1e62d8bc3614b3d5 (patch) | |
| tree | a3a5a291d3949c6be6ab70f929912128325afc15 /src/test | |
| parent | 5e570ce4b087ad7f8623772b64a3d618df9b7756 (diff) | |
| parent | e8cd29ba5ecae71cfde24d2e652b50acf63ea155 (diff) | |
| download | rust-92e265cdea0c528ccca4ccdd1e62d8bc3614b3d5.tar.gz rust-92e265cdea0c528ccca4ccdd1e62d8bc3614b3d5.zip | |
auto merge of #5802 : nikomatsakis/rust/issue-4183-trait-substs, r=nikomatsakis
Cleanup substitutions and treatment of generics around traits in a number of ways - In a TraitRef, use the self type consistently to refer to the Self type: - trait ref in `impl Trait<A,B,C> for S` has a self type of `S`. - trait ref in `A:Trait` has the self type `A` - trait ref associated with a trait decl has self type `Self` - trait ref associated with a supertype has self type `Self` - trait ref in an object type `@Trait` has no self type - Rewrite `each_bound_traits_and_supertraits` to perform substitutions as it goes, and thus yield a series of trait refs that are always in the same 'namespace' as the type parameter bound given as input. Before, we left this to the caller, but this doesn't work because the caller lacks adequare information to perform the type substitutions correctly. - For provided methods, substitute the generics involved in the provided method correctly. - Introduce TypeParameterDef, which tracks the bounds declared on a type parameter and brings them together with the def_id and (in the future) other information (maybe even the parameter's name!). - Introduce Subst trait, which helps to cleanup a lot of the repetitive code involved with doing type substitution. - Introduce Repr trait, which makes debug printouts far more convenient. Fixes #4183. Needed for #5656. r? @catamorphism
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/run-pass/trait-inheritance-self-in-supertype.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/test/run-pass/trait-inheritance-self-in-supertype.rs b/src/test/run-pass/trait-inheritance-self-in-supertype.rs new file mode 100644 index 00000000000..8105cf23d80 --- /dev/null +++ b/src/test/run-pass/trait-inheritance-self-in-supertype.rs @@ -0,0 +1,61 @@ +// Test for issue #4183: use of Self in supertraits. + +pub static FUZZY_EPSILON: float = 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: 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 { + f32::abs(*self - *other) < *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 { + f64::abs(*self - *other) < *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)); +} \ No newline at end of file |
