@@ -22,15 +22,15 @@ pub trait FixedSizeEncoding: Default {
22
22
// FIXME(eddyb) make these generic functions, or at least defaults here.
23
23
// (same problem as above, needs `[u8; Self::BYTE_LEN]`)
24
24
// For now, a macro (`fixed_size_encoding_byte_len_and_defaults`) is used.
25
- fn read_from_bytes_at ( b : & [ u8 ] , i : usize ) -> Self ;
25
+ fn maybe_read_from_bytes_at ( b : & [ u8 ] , i : usize ) -> Option < Self > ;
26
26
fn write_to_bytes_at ( self , b : & mut [ u8 ] , i : usize ) ;
27
27
}
28
28
29
29
// HACK(eddyb) this shouldn't be needed (see comments on the methods above).
30
30
macro_rules! fixed_size_encoding_byte_len_and_defaults {
31
31
( $byte_len: expr) => {
32
32
const BYTE_LEN : usize = $byte_len;
33
- fn read_from_bytes_at ( b: & [ u8 ] , i: usize ) -> Self {
33
+ fn maybe_read_from_bytes_at ( b: & [ u8 ] , i: usize ) -> Option < Self > {
34
34
const BYTE_LEN : usize = $byte_len;
35
35
// HACK(eddyb) ideally this would be done with fully safe code,
36
36
// but slicing `[u8]` with `i * N..` is optimized worse, due to the
@@ -41,7 +41,7 @@ macro_rules! fixed_size_encoding_byte_len_and_defaults {
41
41
b. len( ) / BYTE_LEN ,
42
42
)
43
43
} ;
44
- FixedSizeEncoding :: from_bytes( & b [ i ] )
44
+ b . get ( i ) . map ( |b| FixedSizeEncoding :: from_bytes( b ) )
45
45
}
46
46
fn write_to_bytes_at( self , b: & mut [ u8 ] , i: usize ) {
47
47
const BYTE_LEN : usize = $byte_len;
@@ -126,16 +126,21 @@ pub struct Table<T> where Option<T>: FixedSizeEncoding {
126
126
_marker : PhantomData < T > ,
127
127
}
128
128
129
- impl < T > Table < T > where Option < T > : FixedSizeEncoding {
130
- pub fn new ( len : usize ) -> Self {
129
+ impl < T > Default for Table < T > where Option < T > : FixedSizeEncoding {
130
+ fn default ( ) -> Self {
131
131
Table {
132
- // FIXME(eddyb) only allocate and encode as many entries as needed.
133
- bytes : vec ! [ 0 ; len * <Option <T >>:: BYTE_LEN ] ,
132
+ bytes : vec ! [ ] ,
134
133
_marker : PhantomData ,
135
134
}
136
135
}
136
+ }
137
137
138
+ impl < T > Table < T > where Option < T > : FixedSizeEncoding {
138
139
pub fn set ( & mut self , i : usize , value : T ) {
140
+ let needed = ( i + 1 ) * <Option < T > >:: BYTE_LEN ;
141
+ if self . bytes . len ( ) < needed {
142
+ self . bytes . resize ( needed, 0 ) ;
143
+ }
139
144
Some ( value) . write_to_bytes_at ( & mut self . bytes , i) ;
140
145
}
141
146
@@ -168,7 +173,7 @@ impl<T> Lazy<Table<T>> where Option<T>: FixedSizeEncoding {
168
173
debug ! ( "Table::lookup: index={:?} len={:?}" , i, self . meta) ;
169
174
170
175
let bytes = & metadata. raw_bytes ( ) [ self . position . get ( ) ..] [ ..self . meta ] ;
171
- <Option < T > >:: read_from_bytes_at ( bytes, i)
176
+ <Option < T > >:: maybe_read_from_bytes_at ( bytes, i) ?
172
177
}
173
178
}
174
179
@@ -179,14 +184,16 @@ pub struct PerDefTable<T> where Option<T>: FixedSizeEncoding {
179
184
hi : Table < T > ,
180
185
}
181
186
182
- impl < T > PerDefTable < T > where Option < T > : FixedSizeEncoding {
183
- pub fn new ( ( max_index_lo , max_index_hi ) : ( usize , usize ) ) -> Self {
187
+ impl < T > Default for PerDefTable < T > where Option < T > : FixedSizeEncoding {
188
+ fn default ( ) -> Self {
184
189
PerDefTable {
185
- lo : Table :: new ( max_index_lo ) ,
186
- hi : Table :: new ( max_index_hi ) ,
190
+ lo : Table :: default ( ) ,
191
+ hi : Table :: default ( ) ,
187
192
}
188
193
}
194
+ }
189
195
196
+ impl < T > PerDefTable < T > where Option < T > : FixedSizeEncoding {
190
197
pub fn set ( & mut self , def_id : DefId , value : T ) {
191
198
assert ! ( def_id. is_local( ) ) ;
192
199
let space_index = def_id. index . address_space ( ) . index ( ) ;
0 commit comments