Skip to content

Commit

Permalink
Allow trait objects with methods on self: Gc<Self> on nightly
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
andersk committed Jan 27, 2023
1 parent 7a26eca commit 2ee7dbe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")]
Expand Down Expand Up @@ -56,6 +59,9 @@ pub struct Gc<T: ?Sized + 'static> {
#[cfg(feature = "nightly")]
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Gc<U>> for Gc<T> {}

#[cfg(feature = "nightly")]
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Gc<U>> for Gc<T> {}

impl<T: Trace> Gc<T> {
/// Constructs a new `Gc<T>` with the given value.
///
Expand Down
21 changes: 21 additions & 0 deletions gc/tests/gc_self_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![cfg(feature = "nightly")]
#![feature(arbitrary_self_types)]

use gc::{Finalize, Gc, Trace};

trait Foo: Trace {
fn foo(self: Gc<Self>) {}
}

#[derive(Trace, Finalize)]
struct Bar;

impl Foo for Bar {
fn foo(self: Gc<Bar>) {}
}

#[test]
fn gc_self_method() {
let gc: Gc<dyn Foo> = Gc::new(Bar);
gc.foo();
}

0 comments on commit 2ee7dbe

Please sign in to comment.