diff --git a/content/news/039/index.md b/content/news/039/index.md index 6e085f6c2..06206b21b 100644 --- a/content/news/039/index.md +++ b/content/news/039/index.md @@ -104,6 +104,67 @@ it hit version 0.28 which added new functionality and improved existing: ## Library Updates +### [presser] + +[presser] ([GitHub][presser-github], [docs.rs][presser-docs]) +by [@fu5ha] ([Embark Studios][embark]) +is a crate to help you copy things into raw buffers without +invoking spooky action at a distance (undefined behavior). + +Ever done something like this? + +```rust +#[derive(Clone, Copy)] +#[repr(C)] +struct MyDataStruct { + a: u8, + b: u32, +} + +let my_data = MyDataStruct { a: 0, b: 42 }; + +// 🚨 MyDataStruct contains 3 padding bytes after `a`, which are +// uninit, therefore getting a slice that includes them is UB! +let my_data_bytes: &[u8] = transmute(&my_data); + +// allocate an uninit buffer of some size +let my_buffer: MyBufferType = some_api.alloc_buffer_size(2048); + +// 🚨 this is UB for the same reason, these bytes are uninit!* +let buffer_as_bytes: &mut [u8] = + slice::from_raw_parts(my_buffer.ptr(), my_buffer.size()); + +// 🚨 this is UB because not only are both slices invalid, +// this is not ensuring proper alignment! +buffer_as_bytes.copy_from_slice(my_data_bytes); +``` + +[presser] can help. + +```rust +// borrow our raw allocation as a presser::Slab, asserting we have +// unique access to it. see the docs for more. +let slab = unsafe { raw_allocation.borrow_as_slab(); } + +// now we may safely copy `my_data` into `my_buffer`, +// starting at a minimum offset of 0 into the buffer +let copy_record = presser::copy_to_offset(&my_data, &mut slab, 0)?; +``` + +If you're not convinced this is actually an issue, read more in the +[crate readme][presser-readme]. If you're intrigued and want to know more, +see the [docs][presser-docs]. + +_Discussions: [/r/rust](https://reddit.com/r/rust/comments/y5mq3w/presser), +[Twitter](https://twitter.com/fu5ha/status/1581705656218062848)_ + +[presser]: https://crates.io/crates/presser +[presser-github]: https://github.com/embarkstudios/presser +[presser-docs]: https://docs.rs/presser +[@fu5ha]: https://github.com/fu5ha +[embark]: https://github.com/embarkstudios +[presser-readme]: https://crates.io/crates/presser + ## Popular Workgroup Issues in Github