File: repeated-supertrait.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (48 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//@ run-pass
// Test a case of a trait which extends the same supertrait twice, but
// with difference type parameters. Test that we can invoke the
// various methods in various ways successfully.
// See also `ui/traits/trait-repeated-supertrait-ambig.rs`.


trait CompareTo<T> {
    fn same_as(&self, t: T) -> bool;
}

trait CompareToInts : CompareTo<i64> + CompareTo<u64> {
}

impl CompareTo<i64> for i64 {
    fn same_as(&self, t: i64) -> bool { *self == t }
}

impl CompareTo<u64> for i64 {
    fn same_as(&self, t: u64) -> bool { *self == (t as i64) }
}

impl CompareToInts for i64 { }

fn with_obj(c: &dyn CompareToInts) -> bool {
    c.same_as(22_i64) && c.same_as(22_u64)
}

fn with_trait<C:CompareToInts>(c: &C) -> bool {
    c.same_as(22_i64) && c.same_as(22_u64)
}

fn with_ufcs1<C:CompareToInts>(c: &C) -> bool {
    <dyn CompareToInts>::same_as(c, 22_i64) && <dyn CompareToInts>::same_as(c, 22_u64)
}

fn with_ufcs2<C:CompareToInts>(c: &C) -> bool {
    CompareTo::same_as(c, 22_i64) && CompareTo::same_as(c, 22_u64)
}

fn main() {
    assert_eq!(22_i64.same_as(22_i64), true);
    assert_eq!(22_i64.same_as(22_u64), true);
    assert_eq!(with_trait(&22), true);
    assert_eq!(with_obj(&22), true);
    assert_eq!(with_ufcs1(&22), true);
    assert_eq!(with_ufcs2(&22), true);
}