Skip to content

Commit

Permalink
Add mutable to data field
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Oct 9, 2023
1 parent 41082c7 commit bf029f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions book/src/how_it_works.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ only need the pointer to be valid (which basically means `ptr..ptr+size` should
aligned. So Zngur can use the pointer to the below `data` in those functions:

```C++
alignas(align_value) ::std::array<uint8_t, size_value> data;
alignas(align_value) mutable ::std::array<uint8_t, size_value> data;
```
The `mutable` keyword is equivalent to the `UnsafeCell` in Rust. It allows modifying `const` objects (which happens with
Rust interior mutable types) without triggering UB.
To support running destructors of Rust types in C++, Zngur uses `std::ptr::drop_in_place` which has similar constraints to `read` and
`write`. But to prevent double free, Zngur needs to track if a Rust type is moved out. It does this using a boolean field called
`drop_flag`, which is `false` if the value doesn't need drop (it is uninitialized or moved out from) and otherwise `true`. So a C++ wrapper
Expand All @@ -25,7 +28,7 @@ for a typical Rust type will look like this:
```C++
struct MultiBuf {
private:
alignas(8)::std::array<uint8_t, 32> data;
alignas(8) mutable ::std::array<uint8_t, 32> data;
bool drop_flag;
public:
Expand Down
2 changes: 1 addition & 1 deletion zngur-generator/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public:
r#"
{{
private:
alignas({align}) ::std::array<uint8_t, {size}> data;
alignas({align}) mutable ::std::array<uint8_t, {size}> data;
"#,
)?;
}
Expand Down

0 comments on commit bf029f8

Please sign in to comment.