File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,6 +7,9 @@ pub use reinterpret::ByteRepr;
77mod rc;
88pub use rc:: * ;
99
10+ mod union;
11+ pub use union:: * ;
12+
1013mod fn_ptr;
1114pub use fn_ptr:: FnPtr ;
1215
Original file line number Diff line number Diff line change @@ -1246,6 +1246,15 @@ impl<T: ?Sized> AsPointerDyn<T> for Rc<RefCell<T>> {
12461246
12471247impl < T : ' static > ByteRepr for Ptr < T > { }
12481248
1249+ impl < T > Ptr < T > {
1250+ pub ( crate ) fn reinterpreted ( alloc : Rc < dyn OriginalAlloc > , byte_offset : usize ) -> Self {
1251+ Ptr {
1252+ offset : byte_offset,
1253+ kind : PtrKind :: Reinterpreted ( alloc) ,
1254+ }
1255+ }
1256+ }
1257+
12491258#[ cfg( test) ]
12501259mod tests {
12511260 use super :: * ;
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2022-present INESC-ID.
2+ // Distributed under the MIT license that can be found in the LICENSE file.
3+
4+ use std:: { cell:: RefCell , rc:: Rc } ;
5+
6+ use crate :: Ptr ;
7+ use crate :: reinterpret:: { ByteRepr , OriginalAlloc , SliceOriginalAlloc } ;
8+
9+ pub struct UnionStore {
10+ bytes : Rc < RefCell < Vec < u8 > > > ,
11+ }
12+
13+ impl UnionStore {
14+ pub fn new ( size : usize ) -> Self {
15+ UnionStore {
16+ bytes : Rc :: new ( RefCell :: new ( vec ! [ 0u8 ; size] ) ) ,
17+ }
18+ }
19+
20+ pub fn pod < T : ByteRepr > ( & self , offset : usize ) -> Ptr < T > {
21+ let alloc: Rc < dyn OriginalAlloc > = Rc :: new ( SliceOriginalAlloc {
22+ weak : Rc :: downgrade ( & self . bytes ) ,
23+ } ) ;
24+ Ptr :: reinterpreted ( alloc, offset)
25+ }
26+ }
27+
28+ impl Clone for UnionStore {
29+ fn clone ( & self ) -> Self {
30+ UnionStore {
31+ bytes : Rc :: new ( RefCell :: new ( self . bytes . borrow ( ) . clone ( ) ) ) ,
32+ }
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments