Skip to main content

rust_spec/
drop.rs

1//! Drop behavior classification.
2//!
3//! Drop behavior specifies whether drop glue runs when the type is dropped and what kind, if any.
4use core::{convert::Infallible, ops::Add};
5
6/// Dropping this type does not run drop glue.
7pub enum NoDrop {}
8
9/// The type owns a field or content that needs drop glue.
10pub enum Inner {}
11
12/// This type declares [`Drop`] and has no inner drop requirements.
13pub enum Custom {}
14
15/// A type whose drop glue is required, classified by its source.
16pub struct WithDrop<K: DropKind>(core::marker::PhantomData<K>, Infallible);
17
18#[sealed::sealed]
19pub trait DropKind {}
20
21#[sealed::sealed]
22impl DropKind for Inner {}
23#[sealed::sealed]
24impl DropKind for Custom {}
25
26impl Add<Self> for NoDrop {
27    type Output = Self;
28
29    fn add(self, _: Self) -> Self::Output {
30        unreachable!()
31    }
32}
33
34impl Add<WithDrop<Custom>> for NoDrop {
35    type Output = WithDrop<Inner>;
36
37    fn add(self, _: WithDrop<Custom>) -> Self::Output {
38        unreachable!()
39    }
40}
41
42impl Add<WithDrop<Inner>> for NoDrop {
43    type Output = WithDrop<Inner>;
44
45    fn add(self, _: WithDrop<Inner>) -> Self::Output {
46        unreachable!()
47    }
48}
49
50impl<Rhs> Add<Rhs> for WithDrop<Inner> {
51    type Output = Self;
52
53    fn add(self, _: Rhs) -> Self::Output {
54        unreachable!()
55    }
56}
57
58impl Add<NoDrop> for WithDrop<Custom> {
59    type Output = Self;
60
61    fn add(self, _: NoDrop) -> Self::Output {
62        unreachable!()
63    }
64}
65
66impl Add<Self> for WithDrop<Custom> {
67    type Output = WithDrop<Inner>;
68
69    fn add(self, _: Self) -> Self::Output {
70        unreachable!()
71    }
72}
73
74impl Add<WithDrop<Inner>> for WithDrop<Custom> {
75    type Output = WithDrop<Inner>;
76
77    fn add(self, _: WithDrop<Inner>) -> Self::Output {
78        unreachable!()
79    }
80}