@@ -23,15 +23,15 @@ crate trait FixedSizeEncoding: Default {
23
23
// FIXME(eddyb) make these generic functions, or at least defaults here.
24
24
// (same problem as above, needs `[u8; Self::BYTE_LEN]`)
25
25
// For now, a macro (`fixed_size_encoding_byte_len_and_defaults`) is used.
26
- fn read_from_bytes_at ( b : & [ u8 ] , i : usize ) -> Self ;
26
+ fn maybe_read_from_bytes_at ( b : & [ u8 ] , i : usize ) -> Option < Self > ;
27
27
fn write_to_bytes_at ( self , b : & mut [ u8 ] , i : usize ) ;
28
28
}
29
29
30
30
// HACK(eddyb) this shouldn't be needed (see comments on the methods above).
31
31
macro_rules! fixed_size_encoding_byte_len_and_defaults {
32
32
( $byte_len: expr) => {
33
33
const BYTE_LEN : usize = $byte_len;
34
- fn read_from_bytes_at ( b: & [ u8 ] , i: usize ) -> Self {
34
+ fn maybe_read_from_bytes_at ( b: & [ u8 ] , i: usize ) -> Option < Self > {
35
35
const BYTE_LEN : usize = $byte_len;
36
36
// HACK(eddyb) ideally this would be done with fully safe code,
37
37
// but slicing `[u8]` with `i * N..` is optimized worse, due to the
@@ -42,7 +42,7 @@ macro_rules! fixed_size_encoding_byte_len_and_defaults {
42
42
b. len( ) / BYTE_LEN ,
43
43
)
44
44
} ;
45
- FixedSizeEncoding :: from_bytes( & b [ i ] )
45
+ b . get ( i ) . map ( |b| FixedSizeEncoding :: from_bytes( b ) )
46
46
}
47
47
fn write_to_bytes_at( self , b: & mut [ u8 ] , i: usize ) {
48
48
const BYTE_LEN : usize = $byte_len;
@@ -116,25 +116,29 @@ impl<T: Encodable> FixedSizeEncoding for Option<Lazy<[T]>> {
116
116
/// encoding or decoding all the values eagerly and in-order.
117
117
// FIXME(eddyb) replace `Vec` with `[_]` here, such that `Box<Table<T>>` would be used
118
118
// when building it, and `Lazy<Table<T>>` or `&Table<T>` when reading it.
119
- // Sadly, that doesn't work for `DefPerTable`, which is `(Table<T>, Table<T>)`,
120
- // and so would need two lengths in its metadata, which is not supported yet.
119
+ // (not sure if that is possible given that the `Vec` is being resized now)
121
120
crate struct Table < T > where Option < T > : FixedSizeEncoding {
122
121
// FIXME(eddyb) store `[u8; <Option<T>>::BYTE_LEN]` instead of `u8` in `Vec`,
123
122
// once that starts being allowed by the compiler (i.e. lazy normalization).
124
123
bytes : Vec < u8 > ,
125
124
_marker : PhantomData < T > ,
126
125
}
127
126
128
- impl < T > Table < T > where Option < T > : FixedSizeEncoding {
129
- crate fn new ( len : usize ) -> Self {
127
+ impl < T > Default for Table < T > where Option < T > : FixedSizeEncoding {
128
+ fn default ( ) -> Self {
130
129
Table {
131
- // FIXME(eddyb) only allocate and encode as many entries as needed.
132
- bytes : vec ! [ 0 ; len * <Option <T >>:: BYTE_LEN ] ,
130
+ bytes : vec ! [ ] ,
133
131
_marker : PhantomData ,
134
132
}
135
133
}
134
+ }
136
135
136
+ impl < T > Table < T > where Option < T > : FixedSizeEncoding {
137
137
crate fn set ( & mut self , i : usize , value : T ) {
138
+ let needed = ( i + 1 ) * <Option < T > >:: BYTE_LEN ;
139
+ if self . bytes . len ( ) < needed {
140
+ self . bytes . resize ( needed, 0 ) ;
141
+ }
138
142
Some ( value) . write_to_bytes_at ( & mut self . bytes , i) ;
139
143
}
140
144
@@ -167,7 +171,7 @@ impl<T> Lazy<Table<T>> where Option<T>: FixedSizeEncoding {
167
171
debug ! ( "Table::lookup: index={:?} len={:?}" , i, self . meta) ;
168
172
169
173
let bytes = & metadata. raw_bytes ( ) [ self . position . get ( ) ..] [ ..self . meta ] ;
170
- <Option < T > >:: read_from_bytes_at ( bytes, i)
174
+ <Option < T > >:: maybe_read_from_bytes_at ( bytes, i) ?
171
175
}
172
176
}
173
177
@@ -176,11 +180,13 @@ impl<T> Lazy<Table<T>> where Option<T>: FixedSizeEncoding {
176
180
// and by using `newtype_index!` to define `DefIndex`.
177
181
crate struct PerDefTable < T > ( Table < T > ) where Option < T > : FixedSizeEncoding ;
178
182
179
- impl < T > PerDefTable < T > where Option < T > : FixedSizeEncoding {
180
- crate fn new ( def_index_count : usize ) -> Self {
181
- PerDefTable ( Table :: new ( def_index_count ) )
183
+ impl < T > Default for PerDefTable < T > where Option < T > : FixedSizeEncoding {
184
+ fn default ( ) -> Self {
185
+ PerDefTable ( Table :: default ( ) )
182
186
}
187
+ }
183
188
189
+ impl < T > PerDefTable < T > where Option < T > : FixedSizeEncoding {
184
190
crate fn set ( & mut self , def_id : DefId , value : T ) {
185
191
assert ! ( def_id. is_local( ) ) ;
186
192
self . 0 . set ( def_id. index . index ( ) , value) ;
0 commit comments