Skip to content

Fix future object safety of CloneAny #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,45 @@ pub trait CloneAny: Any {
#[doc(hidden)]
fn clone_any(&self) -> Box<CloneAny>;
#[doc(hidden)]
fn clone_any_send(&self) -> Box<CloneAny + Send> where Self: Send;
unsafe fn clone_any_send(&self) -> Box<CloneAny + Send>;
#[doc(hidden)]
fn clone_any_sync(&self) -> Box<CloneAny + Sync> where Self: Sync;
unsafe fn clone_any_sync(&self) -> Box<CloneAny + Sync>;
#[doc(hidden)]
fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync> where Self: Send + Sync;
unsafe fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync>;
}

struct CloneAnySendSync<T>(T);
unsafe impl<T> Sync for CloneAnySendSync<T> { }
unsafe impl<T> Send for CloneAnySendSync<T> { }

impl<T: 'static + Clone> CloneAny for CloneAnySendSync<T> {
fn clone_any(&self) -> Box<CloneAny> {
Box::new(self.0.clone())
}
unsafe fn clone_any_send(&self) -> Box<CloneAny + Send> {
Box::new(CloneAnySendSync(self.0.clone()))
}
unsafe fn clone_any_sync(&self) -> Box<CloneAny + Sync> {
Box::new(CloneAnySendSync(self.0.clone()))
}
unsafe fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync> {
Box::new(CloneAnySendSync(self.0.clone()))
}
}

impl<T: Any + Clone> CloneAny for T {
fn clone_any(&self) -> Box<CloneAny> { Box::new(self.clone()) }

fn clone_any_send(&self) -> Box<CloneAny + Send> where Self: Send {
Box::new(self.clone())
unsafe fn clone_any_send(&self) -> Box<CloneAny + Send> {
Box::new(CloneAnySendSync(self.clone()))
}

fn clone_any_sync(&self) -> Box<CloneAny + Sync> where Self: Sync {
Box::new(self.clone())
unsafe fn clone_any_sync(&self) -> Box<CloneAny + Sync> {
Box::new(CloneAnySendSync(self.clone()))
}

fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync>
where Self: Send + Sync {
Box::new(self.clone())
unsafe fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync> {
Box::new(CloneAnySendSync(self.clone()))
}
}

Expand All @@ -59,15 +77,21 @@ impl Clone for Box<CloneAny> {
}

impl Clone for Box<CloneAny + Send> {
fn clone(&self) -> Box<CloneAny + Send> { (**self).clone_any_send() }
fn clone(&self) -> Box<CloneAny + Send> {
unsafe { (**self).clone_any_send() }
}
}

impl Clone for Box<CloneAny + Sync> {
fn clone(&self) -> Box<CloneAny + Sync> { (**self).clone_any_sync() }
fn clone(&self) -> Box<CloneAny + Sync> {
unsafe { (**self).clone_any_sync() }
}
}

impl Clone for Box<CloneAny + Send + Sync> {
fn clone(&self) -> Box<CloneAny + Send + Sync> { (**self).clone_any_send_sync() }
fn clone(&self) -> Box<CloneAny + Send + Sync> {
unsafe { (**self).clone_any_send_sync() }
}
}

unsafe impl UnsafeAnyExt for CloneAny {}
Expand Down