From 2ee7dbeb836474064aefb8cb682478417de0a00f Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 26 Jan 2023 23:25:17 -0800 Subject: [PATCH] Allow trait objects with methods on self: Gc on nightly This requires #![feature(arbitrary_self_types)] and nightly 2023-01-27 or later for https://github.com/rust-lang/rust/pull/97373. Signed-off-by: Anders Kaseorg --- gc/src/lib.rs | 10 ++++++++-- gc/tests/gc_self_method.rs | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 gc/tests/gc_self_method.rs diff --git a/gc/src/lib.rs b/gc/src/lib.rs index d2f64bc..b38e4ba 100644 --- a/gc/src/lib.rs +++ b/gc/src/lib.rs @@ -4,7 +4,10 @@ //! It is marked as non-sendable because the garbage collection only occurs //! thread-locally. -#![cfg_attr(feature = "nightly", feature(coerce_unsized, unsize))] +#![cfg_attr( + feature = "nightly", + feature(coerce_unsized, dispatch_from_dyn, unsize) +)] use crate::gc::{GcBox, GcBoxHeader}; use std::alloc::Layout; @@ -21,7 +24,7 @@ use std::rc::Rc; #[cfg(feature = "nightly")] use std::marker::Unsize; #[cfg(feature = "nightly")] -use std::ops::CoerceUnsized; +use std::ops::{CoerceUnsized, DispatchFromDyn}; mod gc; #[cfg(feature = "serde")] @@ -56,6 +59,9 @@ pub struct Gc { #[cfg(feature = "nightly")] impl, U: ?Sized> CoerceUnsized> for Gc {} +#[cfg(feature = "nightly")] +impl, U: ?Sized> DispatchFromDyn> for Gc {} + impl Gc { /// Constructs a new `Gc` with the given value. /// diff --git a/gc/tests/gc_self_method.rs b/gc/tests/gc_self_method.rs new file mode 100644 index 0000000..3e3d897 --- /dev/null +++ b/gc/tests/gc_self_method.rs @@ -0,0 +1,21 @@ +#![cfg(feature = "nightly")] +#![feature(arbitrary_self_types)] + +use gc::{Finalize, Gc, Trace}; + +trait Foo: Trace { + fn foo(self: Gc) {} +} + +#[derive(Trace, Finalize)] +struct Bar; + +impl Foo for Bar { + fn foo(self: Gc) {} +} + +#[test] +fn gc_self_method() { + let gc: Gc = Gc::new(Bar); + gc.foo(); +}