Skip to content

Commit b07dfe8

Browse files
authored
N39: presser (#1182)
1 parent f89f248 commit b07dfe8

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

content/news/039/index.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,67 @@ it hit version 0.28 which added new functionality and improved existing:
104104

105105
## Library Updates
106106

107+
### [presser]
108+
109+
[presser] ([GitHub][presser-github], [docs.rs][presser-docs])
110+
by [@fu5ha] ([Embark Studios][embark])
111+
is a crate to help you copy things into raw buffers without
112+
invoking spooky action at a distance (undefined behavior).
113+
114+
Ever done something like this?
115+
116+
```rust
117+
#[derive(Clone, Copy)]
118+
#[repr(C)]
119+
struct MyDataStruct {
120+
a: u8,
121+
b: u32,
122+
}
123+
124+
let my_data = MyDataStruct { a: 0, b: 42 };
125+
126+
// 🚨 MyDataStruct contains 3 padding bytes after `a`, which are
127+
// uninit, therefore getting a slice that includes them is UB!
128+
let my_data_bytes: &[u8] = transmute(&my_data);
129+
130+
// allocate an uninit buffer of some size
131+
let my_buffer: MyBufferType = some_api.alloc_buffer_size(2048);
132+
133+
// 🚨 this is UB for the same reason, these bytes are uninit!*
134+
let buffer_as_bytes: &mut [u8] =
135+
slice::from_raw_parts(my_buffer.ptr(), my_buffer.size());
136+
137+
// 🚨 this is UB because not only are both slices invalid,
138+
// this is not ensuring proper alignment!
139+
buffer_as_bytes.copy_from_slice(my_data_bytes);
140+
```
141+
142+
[presser] can help.
143+
144+
```rust
145+
// borrow our raw allocation as a presser::Slab, asserting we have
146+
// unique access to it. see the docs for more.
147+
let slab = unsafe { raw_allocation.borrow_as_slab(); }
148+
149+
// now we may safely copy `my_data` into `my_buffer`,
150+
// starting at a minimum offset of 0 into the buffer
151+
let copy_record = presser::copy_to_offset(&my_data, &mut slab, 0)?;
152+
```
153+
154+
If you're not convinced this is actually an issue, read more in the
155+
[crate readme][presser-readme]. If you're intrigued and want to know more,
156+
see the [docs][presser-docs].
157+
158+
_Discussions: [/r/rust](https://reddit.com/r/rust/comments/y5mq3w/presser),
159+
[Twitter](https://twitter.com/fu5ha/status/1581705656218062848)_
160+
161+
[presser]: https://crates.io/crates/presser
162+
[presser-github]: https://github.com/embarkstudios/presser
163+
[presser-docs]: https://docs.rs/presser
164+
[@fu5ha]: https://github.com/fu5ha
165+
[embark]: https://github.com/embarkstudios
166+
[presser-readme]: https://crates.io/crates/presser
167+
107168
## Popular Workgroup Issues in Github
108169

109170
<!-- Up to 10 links to interesting issues -->

0 commit comments

Comments
 (0)