@@ -6,7 +6,9 @@ use crate::slab::Slabbed;
6
6
7
7
#[ repr( C ) ]
8
8
pub struct Array < T > {
9
+ // u32 offset in the slab
9
10
index : u32 ,
11
+ // number of `T` elements in the array
10
12
len : u32 ,
11
13
_phantom : PhantomData < T > ,
12
14
}
@@ -25,11 +27,15 @@ impl<T> Copy for Array<T> {}
25
27
26
28
impl < T > core:: fmt:: Debug for Array < T > {
27
29
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
28
- f. debug_struct ( "Array" )
29
- . field ( "index" , & self . index )
30
- . field ( "len" , & self . len )
31
- . field ( "_phantom" , & self . _phantom )
32
- . finish ( )
30
+ f. debug_struct ( if self . is_null ( ) {
31
+ "Array (null)"
32
+ } else {
33
+ "Array"
34
+ } )
35
+ . field ( "index" , & self . index )
36
+ . field ( "len" , & self . len )
37
+ . field ( "_phantom" , & self . _phantom )
38
+ . finish ( )
33
39
}
34
40
}
35
41
@@ -75,7 +81,7 @@ impl<T: Slabbed> Default for Array<T> {
75
81
}
76
82
}
77
83
78
- impl < T : Slabbed > Array < T > {
84
+ impl < T > Array < T > {
79
85
pub fn new ( index : u32 , len : u32 ) -> Self {
80
86
Self {
81
87
index,
@@ -92,11 +98,18 @@ impl<T: Slabbed> Array<T> {
92
98
self . len == 0
93
99
}
94
100
101
+ pub fn is_null ( & self ) -> bool {
102
+ self . index == u32:: MAX
103
+ }
104
+
95
105
pub fn contains_index ( & self , index : usize ) -> bool {
96
106
index >= self . index as usize && index < ( self . index + self . len ) as usize
97
107
}
98
108
99
- pub fn at ( & self , index : usize ) -> Id < T > {
109
+ pub fn at ( & self , index : usize ) -> Id < T >
110
+ where
111
+ T : Slabbed ,
112
+ {
100
113
if index >= self . len ( ) {
101
114
Id :: NONE
102
115
} else {
@@ -107,4 +120,16 @@ impl<T: Slabbed> Array<T> {
107
120
pub fn starting_index ( & self ) -> usize {
108
121
self . index as usize
109
122
}
123
+
124
+ /// Convert this array into a `u32` array.
125
+ pub fn into_u32_array ( self ) -> Array < u32 >
126
+ where
127
+ T : Slabbed ,
128
+ {
129
+ Array {
130
+ index : self . index ,
131
+ len : self . len * T :: slab_size ( ) as u32 ,
132
+ _phantom : PhantomData ,
133
+ }
134
+ }
110
135
}
0 commit comments