Skip to main content

gc_arena/
static_collect.rs

1use crate::Rootable;
2use crate::collect::Collect;
3
4use alloc::borrow::{Borrow, BorrowMut};
5use core::convert::{AsMut, AsRef};
6use core::ops::{Deref, DerefMut};
7
8/// A wrapper type that implements Collect whenever the contained T is 'static, which is useful in
9/// generic contexts
10#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
11#[repr(transparent)]
12pub struct Static<T: ?Sized>(pub T);
13
14impl<'a, T: ?Sized + 'static> Rootable<'a> for Static<T> {
15    type Root = Static<T>;
16}
17
18unsafe impl<'gc, T: ?Sized + 'static> Collect<'gc> for Static<T> {
19    const NEEDS_TRACE: bool = false;
20}
21
22impl<T> From<T> for Static<T> {
23    fn from(value: T) -> Self {
24        Self(value)
25    }
26}
27
28impl<T: ?Sized> AsRef<T> for Static<T> {
29    fn as_ref(&self) -> &T {
30        &self.0
31    }
32}
33
34impl<T: ?Sized> AsMut<T> for Static<T> {
35    fn as_mut(&mut self) -> &mut T {
36        &mut self.0
37    }
38}
39
40impl<T: ?Sized> Deref for Static<T> {
41    type Target = T;
42    fn deref(&self) -> &Self::Target {
43        &self.0
44    }
45}
46
47impl<T: ?Sized> DerefMut for Static<T> {
48    fn deref_mut(&mut self) -> &mut Self::Target {
49        &mut self.0
50    }
51}
52
53impl<T: ?Sized> Borrow<T> for Static<T> {
54    fn borrow(&self) -> &T {
55        &self.0
56    }
57}
58
59impl<T: ?Sized> BorrowMut<T> for Static<T> {
60    fn borrow_mut(&mut self) -> &mut T {
61        &mut self.0
62    }
63}