@@ -38,32 +38,29 @@ pub trait AllocBytes:
38
38
/// Returns `None` if we ran out of memory on the host.
39
39
fn zeroed ( size : Size , _align : Align ) -> Option < Self > ;
40
40
41
- /// Gives direct access to the raw underlying storage. Must be aligned according to the `align`
42
- /// passed during construction.
41
+ /// Gives direct access to the raw underlying storage.
43
42
///
44
- /// Crucially this pointer will *not* be invalidated by `deref`/`deref_mut` calls!
45
- /// In Tree Borrows terms, this pointer is a parent of the references returned there.
46
- fn get_bytes_unchecked_raw_mut ( & mut self ) -> * mut u8 ;
43
+ /// Crucially this pointer is compatible with:
44
+ /// - other pointers retunred by this method, and
45
+ /// - references returned from `deref()`, as long as there was no write.
46
+ fn as_mut_ptr ( & mut self ) -> * mut u8 ;
47
47
}
48
48
49
- // Default `bytes` for `Allocation` is a `Box<[u8] >`.
50
- impl AllocBytes for Box < [ u8 ] > {
49
+ // Default `bytes` for `Allocation` is a `Vec<u8 >`.
50
+ impl AllocBytes for Vec < u8 > {
51
51
fn from_bytes < ' a > ( slice : impl Into < Cow < ' a , [ u8 ] > > , _align : Align ) -> Self {
52
- Box :: < [ u8 ] > :: from ( slice. into ( ) )
52
+ slice. into ( ) . into_owned ( )
53
53
}
54
54
55
55
fn zeroed ( size : Size , _align : Align ) -> Option < Self > {
56
56
let bytes = Box :: < [ u8 ] > :: try_new_zeroed_slice ( size. bytes_usize ( ) ) . ok ( ) ?;
57
57
// SAFETY: the box was zero-allocated, which is a valid initial value for Box<[u8]>
58
58
let bytes = unsafe { bytes. assume_init ( ) } ;
59
- Some ( bytes)
59
+ Some ( bytes. into ( ) )
60
60
}
61
61
62
- fn get_bytes_unchecked_raw_mut ( & mut self ) -> * mut u8 {
63
- // We didn't actually put the memory at the right alignment, so we can't support this.
64
- // (That's okay, this function is only needed in Miri which implements this trait for its
65
- // own type.)
66
- panic ! ( "raw bytes access not supported" ) ;
62
+ fn as_mut_ptr ( & mut self ) -> * mut u8 {
63
+ Vec :: as_mut_ptr ( self )
67
64
}
68
65
}
69
66
@@ -76,7 +73,7 @@ impl AllocBytes for Box<[u8]> {
76
73
// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
77
74
#[ derive( Clone , Eq , PartialEq , TyEncodable , TyDecodable ) ]
78
75
#[ derive( HashStable ) ]
79
- pub struct Allocation < Prov : Provenance = CtfeProvenance , Extra = ( ) , Bytes = Box < [ u8 ] > > {
76
+ pub struct Allocation < Prov : Provenance = CtfeProvenance , Extra = ( ) , Bytes = Vec < u8 > > {
80
77
/// The actual bytes of the allocation.
81
78
/// Note that the bytes of a pointer represent the offset of the pointer.
82
79
bytes : Bytes ,
@@ -491,19 +488,18 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
491
488
self . provenance . clear ( range, cx) ?;
492
489
493
490
assert ! ( range. end( ) . bytes_usize( ) <= self . bytes. len( ) ) ; // need to do our own bounds-check
494
- // FIXME: actually now that `self.bytes` is a `Box`, this *does* invalidate existing
495
- // immutable aliases at least under Stacked Borrows...
491
+ // Cruciall, we go via `AllocBytes::as_mut_ptr`, not `AllocBytes::deref_mut`.
496
492
let begin_ptr = self . bytes . as_mut_ptr ( ) . wrapping_add ( range. start . bytes_usize ( ) ) ;
497
493
let len = range. end ( ) . bytes_usize ( ) - range. start . bytes_usize ( ) ;
498
494
Ok ( ptr:: slice_from_raw_parts_mut ( begin_ptr, len) )
499
495
}
500
496
501
497
/// This gives direct mutable access to the entire buffer, just exposing their internal state
502
- /// without reseting anything. Directly exposes the `AllocBytes` method of the same name . Only
503
- /// works if `OFFSET_IS_ADDR` is true.
498
+ /// without reseting anything. Directly exposes `AllocBytes::as_mut_ptr` . Only works if
499
+ /// `OFFSET_IS_ADDR` is true.
504
500
pub fn get_bytes_unchecked_raw_mut ( & mut self ) -> * mut u8 {
505
501
assert ! ( Prov :: OFFSET_IS_ADDR ) ;
506
- self . bytes . get_bytes_unchecked_raw_mut ( )
502
+ self . bytes . as_mut_ptr ( )
507
503
}
508
504
}
509
505
0 commit comments