1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! Defines the interface to garbage collected arrays.
use core::ops::{Deref, Index};
use core::ptr::NonNull;
use core::cmp::Ordering;
use core::slice::SliceIndex;
use core::str;
use core::fmt::{self, Formatter, Debug, Display};
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;

use crate::{CollectorId, internals::ConstCollectorId, GcSafe, GcRebrand, Gc};
use zerogc_derive::{Trace, unsafe_gc_impl};

use self::repr::{GcArrayPtr};

pub mod repr;

/// A garbage collected string.
///
/// This is a transparent wrapper around `GcArray<u8>`,
/// with the additional invariant that it's utf8 encoded.
///
/// ## Safety
/// The bytes can be assumed to be UTF8 encoded,
/// just like with a `str`.
///
/// Assuming the bytes are utf8 encoded,
/// this can be transmuted back and forth from `GcArray<u8, Id>`
#[repr(transparent)]
#[derive(Trace, Eq, PartialEq, Hash, Clone, Copy)]
#[zerogc(copy, collector_ids(Id))]
pub struct GcString<'gc, Id: CollectorId> {
    bytes: GcArray<'gc, u8, Id>    
}
impl<'gc, Id: CollectorId> GcString<'gc, Id> {
    /// Convert an array of UTF8 bytes into a string.
    ///
    /// Returns an error if the bytes aren't valid UTF8,
    /// just like [core::str::from_utf8].
    #[inline]
    pub fn from_utf8(bytes: GcArray<'gc, u8, Id>) -> Result<Self, core::str::Utf8Error> {
        core::str::from_utf8(bytes.as_slice())?;
        // SAFETY: Validated with from_utf8 call
        Ok(unsafe { Self::from_utf8_unchecked(bytes) })
    }
    /// Convert an array of UTF8 bytes into a string,
    /// without checking for validity.
    ///
    /// ## Safety
    /// Undefined behavior if the bytes aren't valid
    /// UTF8, just like with [core::str::from_utf8_unchecked]
    #[inline]
    pub const unsafe fn from_utf8_unchecked(bytes: GcArray<'gc, u8, Id>) -> Self {
        GcString { bytes }
    }
    /// Retrieve this string as a raw array of bytes
    #[inline]
    pub const fn as_bytes(&self) -> GcArray<'gc, u8, Id> {
        self.bytes
    }
    /// Convert this string into a slice of bytes
    #[inline]
    pub fn as_str(&self) -> &'gc str {
        unsafe { str::from_utf8_unchecked(self.as_bytes().as_slice()) }
    }
}
/// Const access to [GcString]
pub trait ConstStringAccess<'gc> {
    /// Get this string as a slice of bytes
    fn as_bytes_const(&self) -> &'gc [u8];
    /// Convert this string to a `str` slice
    fn as_str_const(&self) -> &'gc str;
    /// Get the length of this string (in bytes)
    fn len_const(&self) -> usize;
}
impl<'gc, Id: ~const ConstCollectorId> const ConstStringAccess<'gc> for GcString<'gc, Id> {
    #[inline]
    fn as_bytes_const(&self) -> &'gc [u8] {
        self.bytes.as_slice_const()
    }
    #[inline]
    fn as_str_const(&self) -> &'gc str {
        unsafe { str::from_utf8_unchecked(self.as_bytes_const()) }
    }
    #[inline]
    fn len_const(&self) -> usize {
        self.bytes.len_const()
    }
}
impl<'gc, Id: CollectorId> Deref for GcString<'gc, Id> {
    type Target = str;
    #[inline]
    fn deref(&self) -> &'_ str {
        self.as_str()
    }
}
impl<'gc, Id: CollectorId> Debug for GcString<'gc, Id> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Debug::fmt(self.as_str(), f)
    }
}
impl<'gc, Id: CollectorId> Display for GcString<'gc, Id> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(self.as_str(), f)
    }  
}

/// A garbage collected array.
///
/// The length is immutable and cannot change
/// once it has been allocated.
///
/// ## Safety
/// This is a 'repr(transparent)' wrapper arround
/// [GcArrayRepr].
#[repr(transparent)]
pub struct GcArray<'gc, T, Id: CollectorId> {
    ptr: Id::ArrayPtr,
    marker: PhantomData<Gc<'gc, [T], Id>>
}
impl<'gc, T, Id: CollectorId> GcArray<'gc, T, Id> {
    /// Convert this array into a slice
    #[inline]
    pub fn as_slice(&self) -> &'gc [T] {
        unsafe {
            core::slice::from_raw_parts(
                self.as_raw_ptr(),
                self.len()
            )
        }
    }
    /// Load a raw pointer to the array's value
    #[inline]
    pub fn as_raw_ptr(&self) -> *mut T {
        self.ptr.as_raw_ptr() as *mut T
    }
    /// Get the underlying 'Id::ArrayPtr' for this array
    ///
    /// ## Safety
    /// Must not interpret the underlying pointer as the
    /// incorrect type.
    #[inline]
    pub const unsafe fn as_internal_ptr_repr(&self) -> &'_ Id::ArrayPtr {
        &self.ptr
    }
    /// Load the length of the array
    #[inline]
    pub fn len(&self) -> usize {
        match self.ptr.len() {
            Some(len) => len,
            None => Id::resolve_array_len(self),
        }
    }
    /// Check if the array is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Resolve the [CollectorId]
    #[inline]
    pub fn collector_id(&self) -> &'_ Id {
        Id::resolve_array_id(self)
    }
    /// Create an array from the specified raw pointer and length
    ///
    /// ## Safety
    /// Pointer and length must be valid, and point to a garbage collected
    /// value allocated from the corresponding [CollectorId]
    #[inline]
    pub unsafe fn from_raw_ptr(ptr: NonNull<T>, len: usize) -> Self {
        GcArray { ptr: Id::ArrayPtr::from_raw_parts(ptr, len), marker: PhantomData }
    }
}
/// If the underlying type is `Sync`, it's safe
/// to share garbage collected references between threads.
///
/// The safety of the collector itself depends on whether [CollectorId] is Sync.
/// If it is, the whole garbage collection implementation should be as well.
unsafe impl<'gc, T, Id> Sync for GcArray<'gc, T, Id>
    where T: Sync, Id: CollectorId + Sync {}
unsafe impl<'gc, T, Id> Send for GcArray<'gc, T, Id>
    where T: Sync, Id: CollectorId + Sync {}
/// Const access to [GcString]
pub trait ConstArrayAccess<'gc, T> {
    /// The value of the array as a slice
    fn as_slice_const<'a>(&self) -> &'a [T] where 'gc: 'a;
    /// Load a raw pointer to the array's value
    fn as_raw_ptr_const(&self) -> *mut T;
    /// The length of this array
    fn len_const(&self) -> usize;
}
// Relax T: GcSafe bound
impl<'gc, T, Id: ~const ConstCollectorId> const ConstArrayAccess<'gc, T> for GcArray<'gc, T, Id> {
    #[inline]
    fn as_slice_const<'a>(&self) -> &'a [T] where 'gc: 'a {
        /*
         * TODO: This is horrible, but currently nessicarry
         * to do this in a const-fn context.
         */
        match Id::ArrayPtr::UNCHECKED_KIND {
            repr::ArrayPtrKind::Fat => {
                unsafe {
                    core::mem::transmute_copy::<
                        Id::ArrayPtr,
                        &'a [T]
                    >(&self.ptr)
                }
            },
            repr::ArrayPtrKind::Thin => {
                unsafe {
                    let ptr = core::mem::transmute_copy::<
                        Id::ArrayPtr,
                        NonNull<T>
                    >(&self.ptr);
                    &*core::ptr::slice_from_raw_parts(
                        ptr.as_ptr(),
                        Id::resolve_array_len_const(
                            self
                        )
                    )
                }
            },
        }
    }
    /// Load a raw pointer to the array's value
    #[inline]
    fn as_raw_ptr_const(&self) -> *mut T {
        self.as_slice_const().as_ptr() as *mut T
    }
    /// Load the length of the array
    #[inline]
    fn len_const(&self) -> usize {
        self.as_slice_const().len()
    }
}
impl<'gc, T, I, Id: CollectorId> Index<I> for GcArray<'gc, T, Id>
    where I: SliceIndex<[T]> {
    type Output = I::Output;
    #[inline]
    fn index(&self, idx: I) -> &I::Output {
        &self.as_slice()[idx]
    }
}
impl<'gc, T, Id: CollectorId> Deref for GcArray<'gc, T, Id> {
    type Target = [T];

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}
impl<'gc, T, Id: CollectorId> Copy for GcArray<'gc, T, Id> {}
impl<'gc, T, Id: CollectorId> Clone for GcArray<'gc, T, Id> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}
impl<'gc, T: Debug, Id: CollectorId> Debug for GcArray<'gc, T, Id> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}
impl<'gc, T: PartialEq, Id: CollectorId> PartialEq for GcArray<'gc, T, Id> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}
impl<'gc, T: PartialEq, Id: CollectorId> PartialEq<[T]> for GcArray<'gc, T, Id> {
    #[inline]
    fn eq(&self, other: &[T]) -> bool {
        self.as_slice() == other
    }
}
impl<'gc, T: PartialOrd, Id: CollectorId> PartialOrd for GcArray<'gc, T, Id> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.as_slice().partial_cmp(other.as_slice())
    }
}
impl<'gc, T: PartialOrd, Id: CollectorId> PartialOrd<[T]> for GcArray<'gc, T, Id> {
    #[inline]
    fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
        self.as_slice().partial_cmp(other)
    }
}
impl<'gc, T: Ord, Id: CollectorId> Ord for GcArray<'gc, T, Id> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_slice().cmp(other)
    }
}
impl<'gc, T: Eq, Id: CollectorId> Eq for GcArray<'gc, T, Id> {}
impl<'gc, T: Hash, Id: CollectorId> Hash for GcArray<'gc, T, Id> {
    #[inline]
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        T::hash_slice(self.as_slice(), hasher)
    }
}
impl<'gc, T, Id: CollectorId> IntoIterator for GcArray<'gc, T, Id>  where T: 'gc {
    type Item = &'gc T;

    type IntoIter = core::slice::Iter<'gc, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.as_slice().iter()
    }
}
impl<'array, 'gc, T, Id: CollectorId> IntoIterator for &'array GcArray<'gc, T, Id> 
    where T: 'array {
    type Item = &'array T;

    type IntoIter = core::slice::Iter<'array, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.as_slice().iter()
    }
}
// Need to implement by hand, because [T] is not GcRebrand
unsafe_gc_impl!(
    target => GcArray<'gc, T, Id>,
    params => ['gc, T: GcSafe<'gc, Id>, Id: CollectorId],
    bounds => {
        TraceImmutable => never,
        GcRebrand => { where T: GcRebrand<'new_gc, Id>, <T as GcRebrand<'new_gc, Id>>::Branded: Sized + GcSafe<'new_gc, Id> },
    },
    null_trace => never,
    branded_type => GcArray<'new_gc, <T as GcRebrand<'new_gc, Id>>::Branded, Id>,
    NEEDS_TRACE => true,
    NEEDS_DROP => false,
    trace_mut => |self, visitor| {
        unsafe { visitor.trace_array(self) }
    },
    collector_id => Id,
    visit_inside_gc => |gc, visitor| {
        visitor.trace_gc(gc)
    }
);

#[cfg(test)]
mod test {
    use crate::{CollectorId, GcArray};
    use crate::epsilon::{self};
    #[test]
    fn test_covariance<'a>() {
        fn covariant<'a, T, Id: CollectorId>(s: GcArray<'static, T, Id>) -> GcArray<'a, T, Id> {
            s as _
        }
        const SRC: &[u32] = &[1, 2, 5];
        let s: epsilon::GcArray<'static, u32> = epsilon::gc_array(SRC);
        let k: epsilon::GcArray<'a, u32> = covariant(s);
        assert_eq!(k.as_slice(), SRC);
    }
}