|
| 1 | +//! `Closure` trait for examining closure captured variables, and trait implementations for closures |
| 2 | +#![stable(feature = "closure_traits", since = "1.38.0")] |
| 3 | + |
| 4 | +use crate::mem::{transmute, transmute_copy, forget}; |
| 5 | +use crate::fmt::{self, Debug}; |
| 6 | +use crate::cmp::Ordering; |
| 7 | +use crate::hash::{Hash, Hasher}; |
| 8 | + |
| 9 | +/// `Closure` is a trait automatically implemented for closures by the compiler |
| 10 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 11 | +#[lang = "closure_trait"] |
| 12 | +#[fundamental] |
| 13 | +pub unsafe trait Closure: Sized { |
| 14 | + /// The tuple that has equivalent layout to this closure |
| 15 | + #[stable(feature = "closure_traits", since = "1.38.0")] |
| 16 | + type Inner; |
| 17 | +} |
| 18 | + |
| 19 | +/// `ClosureExt` is a trait that allows easier use of `Closure` |
| 20 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 21 | +pub trait ClosureExt: Closure { |
| 22 | + /// Get a reference to the tuple that has same layout as this closure |
| 23 | + #[stable(feature = "closure_traits", since = "1.38.0")] |
| 24 | + fn get(&self) -> &Self::Inner; |
| 25 | + |
| 26 | + /// Get a mutable reference to the tuple that has the same layout as this closure |
| 27 | + #[stable(feature = "closure_traits", since = "1.38.0")] |
| 28 | + fn get_mut(&mut self) -> &mut Self::Inner; |
| 29 | + |
| 30 | + /// Convert self to the tuple that has same layout as this closure |
| 31 | + #[stable(feature = "closure_traits", since = "1.38.0")] |
| 32 | + fn into_inner(self) -> Self::Inner; |
| 33 | + |
| 34 | + /// Create a closure from the tuple that has same layout as this closure |
| 35 | + #[stable(feature = "closure_traits", since = "1.38.0")] |
| 36 | + fn from_inner(x: Self::Inner) -> Self; |
| 37 | +} |
| 38 | + |
| 39 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 40 | +impl<T: Closure> ClosureExt for T { |
| 41 | + fn get(&self) -> &Self::Inner { |
| 42 | + unsafe {transmute(self)} |
| 43 | + } |
| 44 | + |
| 45 | + fn get_mut(&mut self) -> &mut Self::Inner { |
| 46 | + unsafe {transmute(self)} |
| 47 | + } |
| 48 | + |
| 49 | + fn into_inner(self) -> Self::Inner { |
| 50 | + let r = unsafe {transmute_copy(&self)}; |
| 51 | + forget(self); |
| 52 | + r |
| 53 | + } |
| 54 | + |
| 55 | + fn from_inner(x: Self::Inner) -> Self { |
| 56 | + let r = unsafe {transmute_copy(&x)}; |
| 57 | + forget(x); |
| 58 | + r |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 63 | +impl<T: Closure> Debug for T |
| 64 | + where <T as Closure>::Inner: Debug { |
| 65 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 66 | + // the type name is already printed by the Debug impl for LayoutAs |
| 67 | + Debug::fmt(self.get(), f) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// we allow comparisons between versions of the same closure with different |
| 72 | +// type parameters, but not between different closures, thanks to the LayoutAs<T> |
| 73 | +// marker that has no heterogeneous PartialOrd implementation |
| 74 | + |
| 75 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 76 | +impl<A: Closure, B: Closure> PartialEq<B> for A |
| 77 | + where <A as Closure>::Inner: PartialEq<<B as Closure>::Inner> { |
| 78 | + #[inline] |
| 79 | + fn eq(&self, other: &B) -> bool { PartialEq::eq(self.get(), other.get()) } |
| 80 | + #[inline] |
| 81 | + fn ne(&self, other: &B) -> bool { PartialEq::ne(self.get(), other.get()) } |
| 82 | +} |
| 83 | + |
| 84 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 85 | +impl<A: Closure, B: Closure> PartialOrd<B> for A |
| 86 | + where <A as Closure>::Inner: PartialOrd<<B as Closure>::Inner> { |
| 87 | + #[inline] |
| 88 | + fn partial_cmp(&self, other: &B) -> Option<Ordering> { |
| 89 | + PartialOrd::partial_cmp(self.get(), other.get()) |
| 90 | + } |
| 91 | + #[inline] |
| 92 | + fn lt(&self, other: &B) -> bool { PartialOrd::lt(self.get(), other.get()) } |
| 93 | + #[inline] |
| 94 | + fn le(&self, other: &B) -> bool { PartialOrd::le(self.get(), other.get()) } |
| 95 | + #[inline] |
| 96 | + fn ge(&self, other: &B) -> bool { PartialOrd::ge(self.get(), other.get()) } |
| 97 | + #[inline] |
| 98 | + fn gt(&self, other: &B) -> bool { PartialOrd::gt(self.get(), other.get()) } |
| 99 | +} |
| 100 | + |
| 101 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 102 | +impl<T: Closure> Ord for T |
| 103 | + where <T as Closure>::Inner: Ord { |
| 104 | + #[inline] |
| 105 | + fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(self.get(), other.get()) } |
| 106 | +} |
| 107 | + |
| 108 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 109 | +impl<T: Closure> Eq for T |
| 110 | + where <T as Closure>::Inner: Eq { |
| 111 | +} |
| 112 | + |
| 113 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 114 | +impl<T: Closure> Hash for T |
| 115 | + where <T as Closure>::Inner: Hash { |
| 116 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 117 | + self.get().hash(state); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +#[stable(feature = "closure_traits", since = "1.38.0")] |
| 122 | +impl<T: Closure> Default for T |
| 123 | + where <T as Closure>::Inner: Default { |
| 124 | + fn default() -> Self { |
| 125 | + <T as ClosureExt>::from_inner(Default::default()) |
| 126 | + } |
| 127 | +} |
0 commit comments