1use core::clone::TrivialClone;
4use core::cmp::Ordering;
5use core::fmt;
6use core::future::Future;
7use core::hash::{Hash, Hasher};
8use core::marker::{StructuralPartialEq, Tuple};
9use core::ops::{Coroutine, CoroutineState};
10use core::pin::Pin;
11use core::task::{Context, Poll};
12
13#[unstable(feature = "exclusive_wrapper", issue = "98407")]
82#[doc(alias = "SyncWrapper")]
83#[doc(alias = "SyncCell")]
84#[doc(alias = "Unique")]
85#[derive(Default)]
89#[repr(transparent)]
90pub struct Exclusive<T: ?Sized> {
91 inner: T,
92}
93
94#[unstable(feature = "exclusive_wrapper", issue = "98407")]
96unsafe impl<T: ?Sized> Sync for Exclusive<T> {}
97
98#[unstable(feature = "exclusive_wrapper", issue = "98407")]
99impl<T: ?Sized> fmt::Debug for Exclusive<T> {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
101 f.debug_struct("Exclusive").finish_non_exhaustive()
102 }
103}
104
105impl<T: Sized> Exclusive<T> {
106 #[unstable(feature = "exclusive_wrapper", issue = "98407")]
108 #[must_use]
109 #[inline]
110 pub const fn new(t: T) -> Self {
111 Self { inner: t }
112 }
113
114 #[unstable(feature = "exclusive_wrapper", issue = "98407")]
116 #[rustc_const_unstable(feature = "exclusive_wrapper", issue = "98407")]
117 #[must_use]
118 #[inline]
119 pub const fn into_inner(self) -> T {
120 self.inner
121 }
122}
123
124impl<T: ?Sized> Exclusive<T> {
125 #[unstable(feature = "exclusive_wrapper", issue = "98407")]
127 #[must_use]
128 #[inline]
129 pub const fn get_mut(&mut self) -> &mut T {
130 &mut self.inner
131 }
132
133 #[unstable(feature = "exclusive_wrapper", issue = "98407")]
140 #[must_use]
141 #[inline]
142 pub const fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
143 unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }
146 }
147
148 #[unstable(feature = "exclusive_wrapper", issue = "98407")]
152 #[must_use]
153 #[inline]
154 pub const fn from_mut(r: &'_ mut T) -> &'_ mut Exclusive<T> {
155 unsafe { &mut *(r as *mut T as *mut Exclusive<T>) }
157 }
158
159 #[unstable(feature = "exclusive_wrapper", issue = "98407")]
163 #[must_use]
164 #[inline]
165 pub const fn from_pin_mut(r: Pin<&'_ mut T>) -> Pin<&'_ mut Exclusive<T>> {
166 unsafe { Pin::new_unchecked(Self::from_mut(r.get_unchecked_mut())) }
169 }
170}
171
172#[unstable(feature = "exclusive_wrapper", issue = "98407")]
173#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
174impl<T> const From<T> for Exclusive<T> {
175 #[inline]
176 fn from(t: T) -> Self {
177 Self::new(t)
178 }
179}
180
181#[unstable(feature = "exclusive_wrapper", issue = "98407")]
182impl<F, Args> FnOnce<Args> for Exclusive<F>
183where
184 F: FnOnce<Args>,
185 Args: Tuple,
186{
187 type Output = F::Output;
188
189 extern "rust-call" fn call_once(self, args: Args) -> Self::Output {
190 self.into_inner().call_once(args)
191 }
192}
193
194#[unstable(feature = "exclusive_wrapper", issue = "98407")]
195impl<F, Args> FnMut<Args> for Exclusive<F>
196where
197 F: FnMut<Args>,
198 Args: Tuple,
199{
200 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output {
201 self.get_mut().call_mut(args)
202 }
203}
204
205#[unstable(feature = "exclusive_wrapper", issue = "98407")]
206impl<F, Args> Fn<Args> for Exclusive<F>
207where
208 F: Sync + Fn<Args>,
209 Args: Tuple,
210{
211 extern "rust-call" fn call(&self, args: Args) -> Self::Output {
212 self.as_ref().call(args)
213 }
214}
215
216#[unstable(feature = "exclusive_wrapper", issue = "98407")]
217impl<T> Future for Exclusive<T>
218where
219 T: Future + ?Sized,
220{
221 type Output = T::Output;
222
223 #[inline]
224 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
225 self.get_pin_mut().poll(cx)
226 }
227}
228
229#[unstable(feature = "coroutine_trait", issue = "43122")] impl<R, G> Coroutine<R> for Exclusive<G>
231where
232 G: Coroutine<R> + ?Sized,
233{
234 type Yield = G::Yield;
235 type Return = G::Return;
236
237 #[inline]
238 fn resume(self: Pin<&mut Self>, arg: R) -> CoroutineState<Self::Yield, Self::Return> {
239 G::resume(self.get_pin_mut(), arg)
240 }
241}
242
243#[unstable(feature = "exclusive_wrapper", issue = "98407")]
244impl<T> AsRef<T> for Exclusive<T>
245where
246 T: Sync + ?Sized,
247{
248 #[inline]
249 fn as_ref(&self) -> &T {
250 &self.inner
251 }
252}
253
254#[unstable(feature = "exclusive_wrapper", issue = "98407")]
255impl<T> Clone for Exclusive<T>
256where
257 T: Sync + Clone,
258{
259 #[inline]
260 fn clone(&self) -> Self {
261 Self { inner: self.inner.clone() }
262 }
263}
264
265#[doc(hidden)]
266#[unstable(feature = "trivial_clone", issue = "none")]
267unsafe impl<T> TrivialClone for Exclusive<T> where T: Sync + TrivialClone {}
268
269#[unstable(feature = "exclusive_wrapper", issue = "98407")]
270impl<T> Copy for Exclusive<T> where T: Sync + Copy {}
271
272#[unstable(feature = "exclusive_wrapper", issue = "98407")]
273impl<T, U> PartialEq<Exclusive<U>> for Exclusive<T>
274where
275 T: Sync + PartialEq<U> + ?Sized,
276 U: Sync + ?Sized,
277{
278 #[inline]
279 fn eq(&self, other: &Exclusive<U>) -> bool {
280 self.inner == other.inner
281 }
282}
283
284#[unstable(feature = "exclusive_wrapper", issue = "98407")]
285impl<T> StructuralPartialEq for Exclusive<T> where T: Sync + StructuralPartialEq + ?Sized {}
286
287#[unstable(feature = "exclusive_wrapper", issue = "98407")]
288impl<T> Eq for Exclusive<T> where T: Sync + Eq + ?Sized {}
289
290#[unstable(feature = "exclusive_wrapper", issue = "98407")]
291impl<T> Hash for Exclusive<T>
292where
293 T: Sync + Hash + ?Sized,
294{
295 #[inline]
296 fn hash<H: Hasher>(&self, state: &mut H) {
297 Hash::hash(&self.inner, state)
298 }
299}
300
301#[unstable(feature = "exclusive_wrapper", issue = "98407")]
302impl<T, U> PartialOrd<Exclusive<U>> for Exclusive<T>
303where
304 T: Sync + PartialOrd<U> + ?Sized,
305 U: Sync + ?Sized,
306{
307 #[inline]
308 fn partial_cmp(&self, other: &Exclusive<U>) -> Option<Ordering> {
309 self.inner.partial_cmp(&other.inner)
310 }
311}
312
313#[unstable(feature = "exclusive_wrapper", issue = "98407")]
314impl<T> Ord for Exclusive<T>
315where
316 T: Sync + Ord + ?Sized,
317{
318 #[inline]
319 fn cmp(&self, other: &Self) -> Ordering {
320 self.inner.cmp(&other.inner)
321 }
322}