# What shall Sync mean across an .await?

**URL:** <https://internals.rust-lang.org/t/what-shall-sync-mean-across-an-await/12020>\
**Category:** language design\
**Created:** [March 25, 2020, 7:44am UTC](https://internals.rust-lang.org/t/what-shall-sync-mean-across-an-await/12020 "2020-03-25T07:44:25Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 25, 2020, 3:01pm UTC](https://internals.rust-lang.org/t/what-shall-sync-mean-across-an-await/12020/2 "2020-03-25T15:01:37Z")

</div>

> [@rkuhn](#):
>
> While `Sync` means unrestricted sharing of a value between threads, the “sharing” over the lifetime of an async fn body is of a very particular nature: there will be no concurrent access to the value (barring bugs in the underlying async runtime implementation).

I’d be not too sure if it is impossible to create concurrent access with “safe” code. I’m thinking about copying a reference out of the future into thread-local storage or something, but that doesn’t really work with the lifetimes. Maybe it is safe in some sense... hmm...

> [@rkuhn](#):
>
> `unsafe impl Sync` on the grounds that in the case above `Stream` requires a `&mut self` in its `poll_next` function, thereby requiring the caller to guarantee mutual exclusion

This is an interesting idea. It just inspired me to do a wrapper type about which I’m curious if it is:

(a) safe, and

(b) already implemented in some crate.

This wrapper would, I believe, remove the need of using `unsafe` directly, if you wrap the problematic fields inside the `Stream`. Or you could even have the whole stream be contained inside such a wrapper (and then perhaps wrapped again for the `Stream` instance... or even make the wrapper `#[fundamental]` or something):

[(playground)](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1ca9ea5766cb58809b679232d426c1fc)

```rust
#[repr(transparent)]
pub struct SyncWrapper<T: ?Sized> (T);

impl<T> SyncWrapper<T> {
    pub fn new(t: T) -> SyncWrapper<T> { SyncWrapper(t) }
    pub fn into_inner(self) -> T { self.0 }
}

impl<T: ?Sized> SyncWrapper<T> {
    pub fn get_mut(&mut self) -> &mut T { &mut self.0 }
}

// reason for safety: an immutable reference to SyncWrapper<T> is worthless,
// hence it’s safe to share such a reference between threads
unsafe impl<T: ?Sized> Sync for SyncWrapper<T> {}

```

---

_[View the full topic](https://internals.rust-lang.org/t/what-shall-sync-mean-across-an-await/12020)._
