|
| 1 | +#![cfg_attr(feature = "nightly", feature(rustc_attrs))] |
| 2 | +#![cfg_attr(feature = "nightly", allow(internal_features))] |
| 3 | + |
| 4 | +#[cfg(feature = "nightly")] |
| 5 | +#[macro_use] |
| 6 | +extern crate rustc_macros; |
| 7 | + |
| 8 | +/// The movability of a coroutine / closure literal: |
| 9 | +/// whether a coroutine contains self-references, causing it to be `!Unpin`. |
| 10 | +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] |
| 11 | +#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))] |
| 12 | +pub enum Movability { |
| 13 | + /// May contain self-references, `!Unpin`. |
| 14 | + Static, |
| 15 | + /// Must not contain self-references, `Unpin`. |
| 16 | + Movable, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] |
| 20 | +#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))] |
| 21 | +pub enum Mutability { |
| 22 | + // N.B. Order is deliberate, so that Not < Mut |
| 23 | + Not, |
| 24 | + Mut, |
| 25 | +} |
| 26 | + |
| 27 | +impl Mutability { |
| 28 | + pub fn invert(self) -> Self { |
| 29 | + match self { |
| 30 | + Mutability::Mut => Mutability::Not, |
| 31 | + Mutability::Not => Mutability::Mut, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// Returns `""` (empty string) or `"mut "` depending on the mutability. |
| 36 | + pub fn prefix_str(self) -> &'static str { |
| 37 | + match self { |
| 38 | + Mutability::Mut => "mut ", |
| 39 | + Mutability::Not => "", |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Returns `"&"` or `"&mut "` depending on the mutability. |
| 44 | + pub fn ref_prefix_str(self) -> &'static str { |
| 45 | + match self { |
| 46 | + Mutability::Not => "&", |
| 47 | + Mutability::Mut => "&mut ", |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// Returns `""` (empty string) or `"mutably "` depending on the mutability. |
| 52 | + pub fn mutably_str(self) -> &'static str { |
| 53 | + match self { |
| 54 | + Mutability::Not => "", |
| 55 | + Mutability::Mut => "mutably ", |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// Return `true` if self is mutable |
| 60 | + pub fn is_mut(self) -> bool { |
| 61 | + matches!(self, Self::Mut) |
| 62 | + } |
| 63 | + |
| 64 | + /// Return `true` if self is **not** mutable |
| 65 | + pub fn is_not(self) -> bool { |
| 66 | + matches!(self, Self::Not) |
| 67 | + } |
| 68 | +} |
0 commit comments