Skip to content

Commit 7a26eca

Browse files
committed
Remove unnecessary Trace constraints
The Trace constraint is still needed when allocating or deallocating Gc objects, and when mutably borrowing a GcCell, but isn’t strictly needed elsewhere. Signed-off-by: Anders Kaseorg <[email protected]>
1 parent a243a10 commit 7a26eca

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

gc/src/gc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl GcBoxHeader {
109109
}
110110

111111
#[repr(C)] // to justify the layout computations in GcBox::from_box, Gc::from_raw
112-
pub(crate) struct GcBox<T: Trace + ?Sized + 'static> {
112+
pub(crate) struct GcBox<T: ?Sized + 'static> {
113113
header: GcBoxHeader,
114114
data: T,
115115
}
@@ -217,22 +217,26 @@ unsafe fn insert_gcbox(gcbox: NonNull<GcBox<dyn Trace>>) {
217217
});
218218
}
219219

220-
impl<T: Trace + ?Sized> GcBox<T> {
220+
impl<T: ?Sized> GcBox<T> {
221221
/// Returns `true` if the two references refer to the same `GcBox`.
222222
pub(crate) fn ptr_eq(this: &GcBox<T>, other: &GcBox<T>) -> bool {
223223
// Use .header to ignore fat pointer vtables, to work around
224224
// https://github.com/rust-lang/rust/issues/46139
225225
ptr::eq(&this.header, &other.header)
226226
}
227+
}
227228

229+
impl<T: Trace + ?Sized> GcBox<T> {
228230
/// Marks this `GcBox` and marks through its data.
229231
pub(crate) unsafe fn trace_inner(&self) {
230232
if !self.header.is_marked() {
231233
self.header.mark();
232234
self.data.trace();
233235
}
234236
}
237+
}
235238

239+
impl<T: ?Sized> GcBox<T> {
236240
/// Increases the root count on this `GcBox`.
237241
/// Roots prevent the `GcBox` from being destroyed by the garbage collector.
238242
pub(crate) unsafe fn root_inner(&self) {

gc/src/lib.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ pub use crate::gc::{stats, GcStats};
4848
/// A garbage-collected pointer type over an immutable value.
4949
///
5050
/// See the [module level documentation](./) for more details.
51-
pub struct Gc<T: Trace + ?Sized + 'static> {
51+
pub struct Gc<T: ?Sized + 'static> {
5252
ptr_root: Cell<NonNull<GcBox<T>>>,
5353
marker: PhantomData<Rc<T>>,
5454
}
5555

5656
#[cfg(feature = "nightly")]
57-
impl<T: Trace + ?Sized + Unsize<U>, U: Trace + ?Sized> CoerceUnsized<Gc<U>> for Gc<T> {}
57+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Gc<U>> for Gc<T> {}
5858

5959
impl<T: Trace> Gc<T> {
6060
/// Constructs a new `Gc<T>` with the given value.
@@ -99,23 +99,23 @@ impl<T: Trace + ?Sized> Gc<T> {
9999
}
100100
}
101101

102-
impl<T: Trace + ?Sized> Gc<T> {
102+
impl<T: ?Sized> Gc<T> {
103103
/// Returns `true` if the two `Gc`s point to the same allocation.
104104
pub fn ptr_eq(this: &Gc<T>, other: &Gc<T>) -> bool {
105105
GcBox::ptr_eq(this.inner(), other.inner())
106106
}
107107
}
108108

109109
/// Returns the given pointer with its root bit cleared.
110-
unsafe fn clear_root_bit<T: ?Sized + Trace>(ptr: NonNull<GcBox<T>>) -> NonNull<GcBox<T>> {
110+
unsafe fn clear_root_bit<T: ?Sized>(ptr: NonNull<GcBox<T>>) -> NonNull<GcBox<T>> {
111111
let ptr = ptr.as_ptr();
112112
let data = ptr.cast::<u8>();
113113
let addr = data as isize;
114114
let ptr = set_data_ptr(ptr, data.wrapping_offset((addr & !1) - addr));
115115
NonNull::new_unchecked(ptr)
116116
}
117117

118-
impl<T: Trace + ?Sized> Gc<T> {
118+
impl<T: ?Sized> Gc<T> {
119119
fn rooted(&self) -> bool {
120120
self.ptr_root.get().as_ptr().cast::<u8>() as usize & 1 != 0
121121
}
@@ -151,7 +151,7 @@ impl<T: Trace + ?Sized> Gc<T> {
151151
}
152152
}
153153

154-
impl<T: Trace + ?Sized> Gc<T> {
154+
impl<T: ?Sized> Gc<T> {
155155
/// Consumes the `Gc`, returning the wrapped pointer.
156156
///
157157
/// To avoid a memory leak, the pointer must be converted back into a `Gc`
@@ -225,7 +225,7 @@ impl<T: Trace + ?Sized> Gc<T> {
225225
}
226226
}
227227

228-
impl<T: Trace + ?Sized> Finalize for Gc<T> {}
228+
impl<T: ?Sized> Finalize for Gc<T> {}
229229

230230
unsafe impl<T: Trace + ?Sized> Trace for Gc<T> {
231231
#[inline]
@@ -263,7 +263,7 @@ unsafe impl<T: Trace + ?Sized> Trace for Gc<T> {
263263
}
264264
}
265265

266-
impl<T: Trace + ?Sized> Clone for Gc<T> {
266+
impl<T: ?Sized> Clone for Gc<T> {
267267
#[inline]
268268
fn clone(&self) -> Self {
269269
unsafe {
@@ -278,7 +278,7 @@ impl<T: Trace + ?Sized> Clone for Gc<T> {
278278
}
279279
}
280280

281-
impl<T: Trace + ?Sized> Deref for Gc<T> {
281+
impl<T: ?Sized> Deref for Gc<T> {
282282
type Target = T;
283283

284284
#[inline]
@@ -287,7 +287,7 @@ impl<T: Trace + ?Sized> Deref for Gc<T> {
287287
}
288288
}
289289

290-
impl<T: Trace + ?Sized> Drop for Gc<T> {
290+
impl<T: ?Sized> Drop for Gc<T> {
291291
#[inline]
292292
fn drop(&mut self) {
293293
// If this pointer was a root, we should unroot it.
@@ -306,16 +306,16 @@ impl<T: Trace + Default> Default for Gc<T> {
306306
}
307307
}
308308

309-
impl<T: Trace + ?Sized + PartialEq> PartialEq for Gc<T> {
309+
impl<T: ?Sized + PartialEq> PartialEq for Gc<T> {
310310
#[inline(always)]
311311
fn eq(&self, other: &Self) -> bool {
312312
**self == **other
313313
}
314314
}
315315

316-
impl<T: Trace + ?Sized + Eq> Eq for Gc<T> {}
316+
impl<T: ?Sized + Eq> Eq for Gc<T> {}
317317

318-
impl<T: Trace + ?Sized + PartialOrd> PartialOrd for Gc<T> {
318+
impl<T: ?Sized + PartialOrd> PartialOrd for Gc<T> {
319319
#[inline(always)]
320320
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
321321
(**self).partial_cmp(&**other)
@@ -342,32 +342,32 @@ impl<T: Trace + ?Sized + PartialOrd> PartialOrd for Gc<T> {
342342
}
343343
}
344344

345-
impl<T: Trace + ?Sized + Ord> Ord for Gc<T> {
345+
impl<T: ?Sized + Ord> Ord for Gc<T> {
346346
#[inline]
347347
fn cmp(&self, other: &Self) -> Ordering {
348348
(**self).cmp(&**other)
349349
}
350350
}
351351

352-
impl<T: Trace + ?Sized + Hash> Hash for Gc<T> {
352+
impl<T: ?Sized + Hash> Hash for Gc<T> {
353353
fn hash<H: Hasher>(&self, state: &mut H) {
354354
(**self).hash(state);
355355
}
356356
}
357357

358-
impl<T: Trace + ?Sized + Display> Display for Gc<T> {
358+
impl<T: ?Sized + Display> Display for Gc<T> {
359359
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
360360
Display::fmt(&**self, f)
361361
}
362362
}
363363

364-
impl<T: Trace + ?Sized + Debug> Debug for Gc<T> {
364+
impl<T: ?Sized + Debug> Debug for Gc<T> {
365365
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
366366
Debug::fmt(&**self, f)
367367
}
368368
}
369369

370-
impl<T: Trace + ?Sized> fmt::Pointer for Gc<T> {
370+
impl<T: ?Sized> fmt::Pointer for Gc<T> {
371371
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
372372
fmt::Pointer::fmt(&self.inner(), f)
373373
}
@@ -392,13 +392,13 @@ impl<
392392
}
393393
}
394394

395-
impl<T: Trace + ?Sized> std::borrow::Borrow<T> for Gc<T> {
395+
impl<T: ?Sized> std::borrow::Borrow<T> for Gc<T> {
396396
fn borrow(&self) -> &T {
397397
self
398398
}
399399
}
400400

401-
impl<T: Trace + ?Sized> std::convert::AsRef<T> for Gc<T> {
401+
impl<T: ?Sized> std::convert::AsRef<T> for Gc<T> {
402402
fn as_ref(&self) -> &T {
403403
self
404404
}
@@ -491,7 +491,7 @@ pub struct GcCell<T: ?Sized + 'static> {
491491
cell: UnsafeCell<T>,
492492
}
493493

494-
impl<T: Trace> GcCell<T> {
494+
impl<T> GcCell<T> {
495495
/// Creates a new `GcCell` containing `value`.
496496
#[inline]
497497
pub fn new(value: T) -> Self {
@@ -508,7 +508,7 @@ impl<T: Trace> GcCell<T> {
508508
}
509509
}
510510

511-
impl<T: Trace + ?Sized> GcCell<T> {
511+
impl<T: ?Sized> GcCell<T> {
512512
/// Immutably borrows the wrapped value.
513513
///
514514
/// The borrow lasts until the returned `GcCellRef` exits scope.
@@ -524,7 +524,9 @@ impl<T: Trace + ?Sized> GcCell<T> {
524524
Err(e) => panic!("{}", e),
525525
}
526526
}
527+
}
527528

529+
impl<T: Trace + ?Sized> GcCell<T> {
528530
/// Mutably borrows the wrapped value.
529531
///
530532
/// The borrow lasts until the returned `GcCellRefMut` exits scope.
@@ -540,7 +542,9 @@ impl<T: Trace + ?Sized> GcCell<T> {
540542
Err(e) => panic!("{}", e),
541543
}
542544
}
545+
}
543546

547+
impl<T: ?Sized> GcCell<T> {
544548
/// Immutably borrows the wrapped value, returning an error if the value is currently mutably
545549
/// borrowed.
546550
///
@@ -583,7 +587,9 @@ impl<T: Trace + ?Sized> GcCell<T> {
583587
})
584588
}
585589
}
590+
}
586591

592+
impl<T: Trace + ?Sized> GcCell<T> {
587593
/// Mutably borrows the wrapped value, returning an error if the value is currently borrowed.
588594
///
589595
/// The borrow lasts until the returned `GcCellRefMut` exits scope.
@@ -646,7 +652,7 @@ impl std::fmt::Display for BorrowMutError {
646652
}
647653
}
648654

649-
impl<T: Trace + ?Sized> Finalize for GcCell<T> {}
655+
impl<T: ?Sized> Finalize for GcCell<T> {}
650656

651657
unsafe impl<T: Trace + ?Sized> Trace for GcCell<T> {
652658
#[inline]
@@ -926,30 +932,30 @@ impl<'a, T: Trace + ?Sized, U: Display + ?Sized> Display for GcCellRefMut<'a, T,
926932

927933
unsafe impl<T: ?Sized + Send> Send for GcCell<T> {}
928934

929-
impl<T: Trace + Clone> Clone for GcCell<T> {
935+
impl<T: Clone> Clone for GcCell<T> {
930936
#[inline]
931937
fn clone(&self) -> Self {
932938
Self::new(self.borrow().clone())
933939
}
934940
}
935941

936-
impl<T: Trace + Default> Default for GcCell<T> {
942+
impl<T: Default> Default for GcCell<T> {
937943
#[inline]
938944
fn default() -> Self {
939945
Self::new(Default::default())
940946
}
941947
}
942948

943-
impl<T: Trace + ?Sized + PartialEq> PartialEq for GcCell<T> {
949+
impl<T: ?Sized + PartialEq> PartialEq for GcCell<T> {
944950
#[inline(always)]
945951
fn eq(&self, other: &Self) -> bool {
946952
*self.borrow() == *other.borrow()
947953
}
948954
}
949955

950-
impl<T: Trace + ?Sized + Eq> Eq for GcCell<T> {}
956+
impl<T: ?Sized + Eq> Eq for GcCell<T> {}
951957

952-
impl<T: Trace + ?Sized + PartialOrd> PartialOrd for GcCell<T> {
958+
impl<T: ?Sized + PartialOrd> PartialOrd for GcCell<T> {
953959
#[inline(always)]
954960
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
955961
(*self.borrow()).partial_cmp(&*other.borrow())
@@ -976,14 +982,14 @@ impl<T: Trace + ?Sized + PartialOrd> PartialOrd for GcCell<T> {
976982
}
977983
}
978984

979-
impl<T: Trace + ?Sized + Ord> Ord for GcCell<T> {
985+
impl<T: ?Sized + Ord> Ord for GcCell<T> {
980986
#[inline]
981987
fn cmp(&self, other: &GcCell<T>) -> Ordering {
982988
(*self.borrow()).cmp(&*other.borrow())
983989
}
984990
}
985991

986-
impl<T: Trace + ?Sized + Debug> Debug for GcCell<T> {
992+
impl<T: ?Sized + Debug> Debug for GcCell<T> {
987993
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
988994
match self.flags.get().borrowed() {
989995
BorrowState::Unused | BorrowState::Reading => f

gc/src/serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl<'de, T: Deserialize<'de> + Trace> Deserialize<'de> for Gc<T> {
1010
}
1111
}
1212

13-
impl<T: Serialize + Trace> Serialize for Gc<T> {
13+
impl<T: Serialize> Serialize for Gc<T> {
1414
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1515
where
1616
S: Serializer,

0 commit comments

Comments
 (0)