@@ -10,7 +10,7 @@ use crate::{
10
10
use crate :: builder:: BoxedDst ;
11
11
use alloc:: vec:: Vec ;
12
12
use core:: mem:: size_of;
13
- use core:: ops:: Deref ;
13
+ use core:: ops:: { Deref , DerefMut } ;
14
14
15
15
/// Holds the raw bytes of a boot information built with [`InformationBuilder`]
16
16
/// on the heap. The bytes returned by [`BootInformationBytes::as_bytes`] are
@@ -33,6 +33,15 @@ impl BootInformationBytes {
33
33
assert_eq ! ( slice. as_ptr( ) . align_offset( 8 ) , 0 ) ;
34
34
slice
35
35
}
36
+
37
+ /// Returns the bytes mutably. They are guaranteed to be correctly aligned.
38
+ pub fn as_bytes_mut ( & mut self ) -> & mut [ u8 ] {
39
+ let slice = & mut self . bytes [ self . offset ..self . offset + self . structure_len ] ;
40
+ // At this point, the alignment is guaranteed. If not, something is
41
+ // broken fundamentally.
42
+ assert_eq ! ( slice. as_ptr( ) . align_offset( 8 ) , 0 ) ;
43
+ slice
44
+ }
36
45
}
37
46
38
47
impl Deref for BootInformationBytes {
@@ -43,6 +52,12 @@ impl Deref for BootInformationBytes {
43
52
}
44
53
}
45
54
55
+ impl DerefMut for BootInformationBytes {
56
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
57
+ self . as_bytes_mut ( )
58
+ }
59
+ }
60
+
46
61
/// Builder to construct a valid Multiboot2 information dynamically at runtime.
47
62
/// The tags will appear in the order of their corresponding enumeration,
48
63
/// except for the END tag.
@@ -411,10 +426,10 @@ mod tests {
411
426
#[ cfg_attr( miri, ignore) ]
412
427
fn test_builder ( ) {
413
428
// Step 1/2: Build MBI
414
- let mb2i_data = create_builder ( ) . build ( ) ;
429
+ let mut mb2i_data = create_builder ( ) . build ( ) ;
415
430
416
431
// Step 2/2: Test the built MBI
417
- let mb2i = unsafe { BootInformation :: load ( mb2i_data. as_ptr ( ) . cast ( ) ) }
432
+ let mb2i = unsafe { BootInformation :: load ( mb2i_data. as_mut_ptr ( ) . cast ( ) ) }
418
433
. expect ( "generated information should be readable" ) ;
419
434
420
435
assert_eq ! ( mb2i. basic_memory_info_tag( ) . unwrap( ) . memory_lower( ) , 640 ) ;
0 commit comments