From a12f040166f8c0a08262badec2bc68f5bcc879e8 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Wed, 2 Nov 2022 22:31:21 +0100 Subject: [PATCH 1/2] add presser to newsletter 39 --- content/news/039/index.md | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/content/news/039/index.md b/content/news/039/index.md index 6e085f6c2..ec2b8c1ef 100644 --- a/content/news/039/index.md +++ b/content/news/039/index.md @@ -104,6 +104,68 @@ it hit version 0.28 which added new functionality and improved existing: ## Library Updates +### [presser] + +[presser] ([GitHub] [docs.rs]) by [@fu5ha] ([Embark Studios]) +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]. If you're intrigued and want to know more, +see the [docs]. + +_Discussions: [/r/rust], [Twitter]_ + +[presser]: https://crates.io/crates/presser +[GitHub]: https://github.com/embarkstudios/presser +[docs.rs]: https://docs.rs/presser +[@fu5ha]: https://github.com/fu5ha +[Embark Studios]: https://github.com/embarkstudios +[crate readme]: https://crates.io/crates/presser +[docs]: https://docs.rs/presser +[Twitter]: https://twitter.com/fu5ha/status/1581705656218062848 +[/r/rust]: https://www.reddit.com/r/rust/comments/y5mq3w/presser_a_crate_to_help_you_copy_things_into_raw/ + ## Popular Workgroup Issues in Github From 6c8ffc782e3d7d1e33bdfe6f66b89b13ad845ff1 Mon Sep 17 00:00:00 2001 From: Andrey Lesnikov Date: Thu, 3 Nov 2022 13:01:57 +0400 Subject: [PATCH 2/2] N39: presser: fmt tweaks --- content/news/039/index.md | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/content/news/039/index.md b/content/news/039/index.md index ec2b8c1ef..06206b21b 100644 --- a/content/news/039/index.md +++ b/content/news/039/index.md @@ -106,7 +106,8 @@ it hit version 0.28 which added new functionality and improved existing: ### [presser] -[presser] ([GitHub] [docs.rs]) by [@fu5ha] ([Embark Studios]) +[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). @@ -151,20 +152,18 @@ 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]. If you're intrigued and want to know more, -see the [docs]. +[crate readme][presser-readme]. If you're intrigued and want to know more, +see the [docs][presser-docs]. -_Discussions: [/r/rust], [Twitter]_ +_Discussions: [/r/rust](https://reddit.com/r/rust/comments/y5mq3w/presser), +[Twitter](https://twitter.com/fu5ha/status/1581705656218062848)_ [presser]: https://crates.io/crates/presser -[GitHub]: https://github.com/embarkstudios/presser -[docs.rs]: https://docs.rs/presser +[presser-github]: https://github.com/embarkstudios/presser +[presser-docs]: https://docs.rs/presser [@fu5ha]: https://github.com/fu5ha -[Embark Studios]: https://github.com/embarkstudios -[crate readme]: https://crates.io/crates/presser -[docs]: https://docs.rs/presser -[Twitter]: https://twitter.com/fu5ha/status/1581705656218062848 -[/r/rust]: https://www.reddit.com/r/rust/comments/y5mq3w/presser_a_crate_to_help_you_copy_things_into_raw/ +[embark]: https://github.com/embarkstudios +[presser-readme]: https://crates.io/crates/presser ## Popular Workgroup Issues in Github