Skip to content

MikailBag/weird-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Syncing unsyncable

This library shows a case when you can safely implement Sync for your type without imposing Sync bound on generic parameters. See WeirdMutex documentation for more details.

Example

Imagine that yoi have a struct that stores some future in it. In this example we will use boxed future, but the same would apply to taking future type as a generic parameter.

use std::{pin::Pin, future::Future};
struct Thing {
   fut: Pin<Box<dyn Future<Output=<()> + Send>>>,
   // ...
}

Unfortunately, this type is not Sync so it can be unergonomic to use with some libraries that require Sync. Naive solution is to write something like this:

use std::{pin::Pin, future::Future};
struct Thing {
   fut: Pin<Box<dyn Future<Output=<()> + Send + Sync>>>,
   // ...
}

Unfortunately, now we impose additional restricitons on the stored future. For example, future using Cell type can no longer be stored in our Thing. Better solution is to use WeirdMutex:

use std::{pin::Pin, future::Future};
struct Thing {
   fut: weird_mutex::WeirdMutex<Pin<Box<dyn Future<Output=<()> + Send>>>>,
   // ...
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages