8000 add presser to newsletter 39 · fu5ha/rust-gamedev.github.io@a12f040 · GitHub
[go: up one dir, main page]

Skip to content

Commit a12f040

Browse files
committed
add presser to newsletter 39
1 parent f89f248 commit a12f040

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

content/news/039/index.md

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

105105
## Library Updates
106106

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

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

0 commit comments

Comments
 (0)
0