@@ -23,15 +23,15 @@ crate trait FixedSizeEncoding: Default {
2323 // FIXME(eddyb) make these generic functions, or at least defaults here.
2424 // (same problem as above, needs `[u8; Self::BYTE_LEN]`)
2525 // 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 > ;
2727 fn write_to_bytes_at ( self , b : & mut [ u8 ] , i : usize ) ;
2828}
2929
3030// HACK(eddyb) this shouldn't be needed (see comments on the methods above).
3131macro_rules! fixed_size_encoding_byte_len_and_defaults {
3232 ( $byte_len: expr) => {
3333 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 > {
3535 const BYTE_LEN : usize = $byte_len;
3636 // HACK(eddyb) ideally this would be done with fully safe code,
3737 // 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 {
4242 b. len( ) / BYTE_LEN ,
4343 )
4444 } ;
45- FixedSizeEncoding :: from_bytes( & b [ i ] )
45+ b . get ( i ) . map ( |b| FixedSizeEncoding :: from_bytes( b ) )
4646 }
4747 fn write_to_bytes_at( self , b: & mut [ u8 ] , i: usize ) {
4848 const BYTE_LEN : usize = $byte_len;
@@ -116,25 +116,29 @@ impl<T: Encodable> FixedSizeEncoding for Option<Lazy<[T]>> {
116116/// encoding or decoding all the values eagerly and in-order.
117117// FIXME(eddyb) replace `Vec` with `[_]` here, such that `Box<Table<T>>` would be used
118118// 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)
121120crate struct Table < T > where Option < T > : FixedSizeEncoding {
122121 // FIXME(eddyb) store `[u8; <Option<T>>::BYTE_LEN]` instead of `u8` in `Vec`,
123122 // once that starts being allowed by the compiler (i.e. lazy normalization).
124123 bytes : Vec < u8 > ,
125124 _marker : PhantomData < T > ,
126125}
127126
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 {
130129 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 ! [ ] ,
133131 _marker : PhantomData ,
134132 }
135133 }
134+ }
136135
136+ impl < T > Table < T > where Option < T > : FixedSizeEncoding {
137137 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+ }
138142 Some ( value) . write_to_bytes_at ( & mut self . bytes , i) ;
139143 }
140144
@@ -167,7 +171,7 @@ impl<T> Lazy<Table<T>> where Option<T>: FixedSizeEncoding {
167171 debug ! ( "Table::lookup: index={:?} len={:?}" , i, self . meta) ;
168172
169173 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) ?
171175 }
172176}
173177
@@ -176,11 +180,13 @@ impl<T> Lazy<Table<T>> where Option<T>: FixedSizeEncoding {
176180// and by using `newtype_index!` to define `DefIndex`.
177181crate struct PerDefTable < T > ( Table < T > ) where Option < T > : FixedSizeEncoding ;
178182
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 ( ) )
182186 }
187+ }
183188
189+ impl < T > PerDefTable < T > where Option < T > : FixedSizeEncoding {
184190 crate fn set ( & mut self , def_id : DefId , value : T ) {
185191 assert ! ( def_id. is_local( ) ) ;
186192 self . 0 . set ( def_id. index . index ( ) , value) ;
0 commit comments