@@ -261,18 +261,26 @@ where
261
261
///
262
262
/// Equivalent to `&seq[..]`.
263
263
pub fn as_slice ( & self ) -> & [ T ] {
264
- // SAFETY: self.data points to self.size consecutive, initialized elements and
265
- // isn't modified externally.
266
- unsafe { std:: slice:: from_raw_parts ( self . data , self . size ) }
264
+ if self . data . is_null ( ) {
265
+ & [ ]
266
+ } else {
267
+ // SAFETY: self.data is not null and points to self.size consecutive,
268
+ // initialized elements and isn't modified externally.
269
+ unsafe { std:: slice:: from_raw_parts ( self . data , self . size ) }
270
+ }
267
271
}
268
272
269
273
/// Extracts a mutable slice containing the entire sequence.
270
274
///
271
275
/// Equivalent to `&mut seq[..]`.
272
276
pub fn as_mut_slice ( & mut self ) -> & mut [ T ] {
273
- // SAFETY: self.data points to self.size consecutive, initialized elements and
274
- // isn't modified externally.
275
- unsafe { std:: slice:: from_raw_parts_mut ( self . data , self . size ) }
277
+ if self . data . is_null ( ) {
278
+ & mut [ ]
279
+ } else {
280
+ // SAFETY: self.data is not null and points to self.size consecutive,
281
+ // initialized elements and isn't modified externally.
282
+ unsafe { std:: slice:: from_raw_parts_mut ( self . data , self . size ) }
283
+ }
276
284
}
277
285
}
278
286
@@ -666,6 +674,12 @@ mod tests {
666
674
}
667
675
}
668
676
677
+ #[ test]
678
+ fn test_empty_sequence ( ) {
679
+ assert ! ( Sequence :: <i32 >:: default ( ) . is_empty( ) ) ;
680
+ assert ! ( BoundedSequence :: <i32 , 5 >:: default ( ) . is_empty( ) ) ;
681
+ }
682
+
669
683
quickcheck ! {
670
684
fn test_extend( xs: Vec <i32 >, ys: Vec <i32 >) -> bool {
671
685
let mut xs_seq = Sequence :: new( xs. len( ) ) ;
0 commit comments