File: equality-rpass.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 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; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (48 lines) | stat: -rw-r--r-- 1,011 bytes parent folder | download | duplicates (5)
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

#![feature(specialization)] //~ WARN the feature `specialization` is incomplete

trait Foo: std::fmt::Debug + Eq {}

impl<T: std::fmt::Debug + Eq> Foo for T {}

fn hide<T: Foo>(x: T) -> impl Foo {
    x
}

trait Leak<T>: Sized {
    fn leak(self) -> T;
}
impl<T, U> Leak<T> for U {
    default fn leak(self) -> T { panic!("type mismatch") }
}
impl<T> Leak<T> for T {
    fn leak(self) -> T { self }
}

trait CheckIfSend: Sized {
    type T: Default;
    fn check(self) -> Self::T { Default::default() }
}
impl<T> CheckIfSend for T {
    default type T = ();
}
impl<T: Send> CheckIfSend for T {
    type T = bool;
}

fn lucky_seven() -> impl Fn(usize) -> u8 {
    let a = [1, 2, 3, 4, 5, 6, 7];
    move |i| a[i]
}

fn main() {
    assert_eq!(hide(42), hide(42));

    assert_eq!(std::mem::size_of_val(&hide([0_u8; 5])), 5);
    assert_eq!(std::mem::size_of_val(&lucky_seven()), 7);

    assert_eq!(Leak::<i32>::leak(hide(5_i32)), 5_i32);

    assert_eq!(CheckIfSend::check(hide(0_i32)), false);
}