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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/// 一种类型的值是否可以转换为另一种类型的值?
///
/// 当 `Self` 类型的任何值的位在给定的 `Context` 中可以安全地转换为 `Dst` 类型的值时,此 trait 由编译器针对 `Src` 和 `Self` 类型即时实现,尽管您已要求编译器进行任何安全检查对 [`Assume`] 很满意。
///
///
#[unstable(feature = "transmutability", issue = "99571")]
#[lang = "transmute_trait"]
pub unsafe trait BikeshedIntrinsicFrom<Src, Context, const ASSUME: Assume = { Assume::NOTHING }>
where
    Src: ?Sized,
{
}

/// 编译器应假定 *你* 正在检查哪些转换安全条件?
#[unstable(feature = "transmutability", issue = "99571")]
#[lang = "transmute_opts"]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct Assume {
    /// 当 `true` 时,编译器假定 *你* 确保 (动态或静态) 目标引用对象没有比源引用对象更严格的对齐要求。
    ///
    pub alignment: bool,

    /// 当 `true` 时,编译器假设 *你* 确保生命周期不会以违反 Rust 内存模型的方式扩展。
    ///
    pub lifetimes: bool,

    /// 当 `true` 时,编译器假定 *你* 已确保您可以安全地违反目标类型 (有时也是源类型) 的类型和字段隐私。
    ///
    pub safety: bool,

    /// 当 `true` 时,编译器假定 *你* 确保源类型实际上是目标类型的有效实例。
    ///
    pub validity: bool,
}

impl Assume {
    /// 不要假设*您*已确保满足任何安全属性。
    #[unstable(feature = "transmutability", issue = "99571")]
    pub const NOTHING: Self =
        Self { alignment: false, lifetimes: false, safety: false, validity: false };

    /// 仅假设满足对齐条件。
    #[unstable(feature = "transmutability", issue = "99571")]
    pub const ALIGNMENT: Self = Self { alignment: true, ..Self::NOTHING };

    /// 假设仅满足生命周期条件。
    #[unstable(feature = "transmutability", issue = "99571")]
    pub const LIFETIMES: Self = Self { lifetimes: true, ..Self::NOTHING };

    /// 仅假设满足安全条件。
    #[unstable(feature = "transmutability", issue = "99571")]
    pub const SAFETY: Self = Self { safety: true, ..Self::NOTHING };

    /// 仅假设满足动态可满足的有效性条件。
    #[unstable(feature = "transmutability", issue = "99571")]
    pub const VALIDITY: Self = Self { validity: true, ..Self::NOTHING };

    /// 假设 `self` 和 `other_assumptions`。
    #[unstable(feature = "transmutability", issue = "99571")]
    pub const fn and(self, other_assumptions: Self) -> Self {
        Self {
            alignment: self.alignment || other_assumptions.alignment,
            lifetimes: self.lifetimes || other_assumptions.lifetimes,
            safety: self.safety || other_assumptions.safety,
            validity: self.validity || other_assumptions.validity,
        }
    }

    /// 假设 `self`,`other_assumptions` 除外。
    #[unstable(feature = "transmutability", issue = "99571")]
    pub const fn but_not(self, other_assumptions: Self) -> Self {
        Self {
            alignment: self.alignment && !other_assumptions.alignment,
            lifetimes: self.lifetimes && !other_assumptions.lifetimes,
            safety: self.safety && !other_assumptions.safety,
            validity: self.validity && !other_assumptions.validity,
        }
    }
}

// FIXME(jswrenn): 这个 const op 实际上是不可用的。Why?
// https://github.com/rust-lang/rust/pull/100726#issuecomment-1219928926
#[unstable(feature = "transmutability", issue = "99571")]
impl core::ops::Add for Assume {
    type Output = Assume;

    fn add(self, other_assumptions: Assume) -> Assume {
        self.and(other_assumptions)
    }
}

// FIXME(jswrenn): 这个 const op 实际上是不可用的。Why?
// https://github.com/rust-lang/rust/pull/100726#issuecomment-1219928926
#[unstable(feature = "transmutability", issue = "99571")]
impl core::ops::Sub for Assume {
    type Output = Assume;

    fn sub(self, other_assumptions: Assume) -> Assume {
        self.but_not(other_assumptions)
    }
}