11
11
mod layout;
12
12
mod alloc;
13
13
14
- use crate :: {
15
- CollectorId , internals:: ConstCollectorId ,
16
- GcContext , GcSafe , GcSimpleAlloc , GcSystem ,
17
- GcVisitor , NullTrace , Trace , TraceImmutable ,
18
- TrustedDrop
19
- } ;
14
+ use crate :: { CollectorId , GcContext , GcSafe , GcSimpleAlloc , GcSystem , GcVisitor , NullTrace , Trace , TraceImmutable , TrustedDrop , internals:: ConstCollectorId } ;
20
15
use std:: ptr:: NonNull ;
21
16
use std:: alloc:: Layout ;
22
17
use std:: rc:: Rc ;
@@ -48,7 +43,7 @@ pub const fn gc<'gc, T: GcSafe<'gc, EpsilonCollectorId> + 'gc>(ptr: &'gc T) -> G
48
43
/// Coerce a slice into a `GcArray`.
49
44
///
50
45
/// This is only supported on the epsilon collector.
51
- /// Because the epsilon collector never allocates ,
46
+ /// Because the epsilon collector never collects ,
52
47
/// it doesn't need to make a distinction between `GcArray<T>` and `&[T]`.
53
48
///
54
49
/// See also: [gc] for converting `&T` -> `Gc<T>`
@@ -64,6 +59,27 @@ pub const fn gc_array<'gc, T: GcSafe<'gc, EpsilonCollectorId> + 'gc>(slice: &'gc
64
59
unsafe { std:: mem:: transmute :: < & ' gc [ T ] , crate :: GcArray < ' gc , T , EpsilonCollectorId > > ( slice) }
65
60
}
66
61
62
+ /// Coerce a `&str` into a `GcString`
63
+ ///
64
+ /// This is only supported on the epsilon collector,
65
+ /// because the epsilon collector never collects.
66
+ ///
67
+ /// See also [gc_array] for converting `&[T]` -> `GcArray<T>`
68
+ #[ inline]
69
+ pub const fn gc_str < ' gc > ( s : & ' gc str ) -> GcString < ' gc > {
70
+ /*
71
+ * SAFETY: Epsilon uses the 'fat' representation for GcArrays.
72
+ * This means that repr(GcArray) == repr(&[T])
73
+ *
74
+ * Because we already know the string is UTF8 encoded,
75
+ * we can take advantage of the fact that repr(str) == repr(&[u8])
76
+ * and repr(GcArray) == repr(GcString).
77
+ * Instead of going `str -> &[T] -> GcArray -> GcString`
78
+ * we can just go directly from `str -> GcString`
79
+ */
80
+ unsafe { std:: mem:: transmute :: < & ' gc str , crate :: array:: GcString < ' gc , EpsilonCollectorId > > ( s) }
81
+ }
82
+
67
83
/// Allocate a [(fake) Gc](Gc) that points to the specified
68
84
/// value and leak it.
69
85
///
@@ -78,7 +94,7 @@ pub fn leaked<'gc, T: GcSafe<'gc, EpsilonCollectorId> + 'static>(value: T) -> Gc
78
94
///
79
95
/// **WARNING**: This never actually collects any garbage
80
96
pub type Gc < ' gc , T > = crate :: Gc < ' gc , T , EpsilonCollectorId > ;
81
- /// A [garbage collected array](`crate::vec ::GcArray`)
97
+ /// A [garbage collected array](`crate::array ::GcArray`)
82
98
/// that uses the [epsilon collector](EpsilonSystem)
83
99
///
84
100
/// **WARNING**: This never actually collects any garbage.
@@ -88,6 +104,11 @@ pub type GcArray<'gc, T> = crate::array::GcArray<'gc, T, EpsilonCollectorId>;
88
104
///
89
105
/// **WARNING**: This never actually collects any garbage.
90
106
pub type GcVec < ' gc , T > = crate :: vec:: GcVec < ' gc , T , EpsilonContext > ;
107
+ /// A [garbage collected string](`crate::array::GcString`)
108
+ /// that uses the epsilon collector.
109
+ ///
110
+ /// **WARNING**: This never actually collects any garbage
111
+ pub type GcString < ' gc > = crate :: array:: GcString < ' gc , EpsilonCollectorId > ;
91
112
92
113
/// A never-collecting garbage collector context.
93
114
///
0 commit comments