Skip to content

Commit 2ee7dbe

Browse files
committed
Allow trait objects with methods on self: Gc<Self> on nightly
This requires #![feature(arbitrary_self_types)] and nightly 2023-01-27 or later for rust-lang/rust#97373. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
1 parent 7a26eca commit 2ee7dbe

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

gc/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
//! It is marked as non-sendable because the garbage collection only occurs
55
//! thread-locally.
66
7-
#![cfg_attr(feature = "nightly", feature(coerce_unsized, unsize))]
7+
#![cfg_attr(
8+
feature = "nightly",
9+
feature(coerce_unsized, dispatch_from_dyn, unsize)
10+
)]
811

912
use crate::gc::{GcBox, GcBoxHeader};
1013
use std::alloc::Layout;
@@ -21,7 +24,7 @@ use std::rc::Rc;
2124
#[cfg(feature = "nightly")]
2225
use std::marker::Unsize;
2326
#[cfg(feature = "nightly")]
24-
use std::ops::CoerceUnsized;
27+
use std::ops::{CoerceUnsized, DispatchFromDyn};
2528

2629
mod gc;
2730
#[cfg(feature = "serde")]
@@ -56,6 +59,9 @@ pub struct Gc<T: ?Sized + 'static> {
5659
#[cfg(feature = "nightly")]
5760
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Gc<U>> for Gc<T> {}
5861

62+
#[cfg(feature = "nightly")]
63+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Gc<U>> for Gc<T> {}
64+
5965
impl<T: Trace> Gc<T> {
6066
/// Constructs a new `Gc<T>` with the given value.
6167
///

gc/tests/gc_self_method.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![cfg(feature = "nightly")]
2+
#![feature(arbitrary_self_types)]
3+
4+
use gc::{Finalize, Gc, Trace};
5+
6+
trait Foo: Trace {
7+
fn foo(self: Gc<Self>) {}
8+
}
9+
10+
#[derive(Trace, Finalize)]
11+
struct Bar;
12+
13+
impl Foo for Bar {
14+
fn foo(self: Gc<Bar>) {}
15+
}
16+
17+
#[test]
18+
fn gc_self_method() {
19+
let gc: Gc<dyn Foo> = Gc::new(Bar);
20+
gc.foo();
21+
}

0 commit comments

Comments
 (0)