Skip to content

Commit eccc08c

Browse files
authored
Add read helpers (#5)
Motivated by Traverse-Research/gpu-allocator#139, scaffolded some read-helper functions. These helpers are mostly all unsafe and have both semi-checked and completely unchecked variants. They are relevant in either case because they lay out in documentation exactly what the needed safety requirements are to read the given data, given that we have a properly implemented `Slab`, and try to remove some common footguns (alignment, size within allocation) where possible in the checked variants. A note for reviewers is that you can skip the `copy.rs` file as that is just code movement from the old `lib.rs`. @eddyb, requested your review since you helped validate `presser` originally and you might have some valuable input on the safety comments/requirements here.
1 parent b1e89d6 commit eccc08c

5 files changed

Lines changed: 1249 additions & 469 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Add `[]_exact` versions of copy functions which ensure that the copy will actually start at the
1414
provided start_offset, otherwise returning an error.
1515
- Add `CopyError::RequestedOffsetUnaligned` to support the above error case.
16+
- Add `read_[]` and `get_maybe_uninit_[]_mut` helper functions for accessing copied data.
1617

1718
## [0.3.1] - 2022-10-16
1819

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# `🗜 presser`
1010

11-
**Utilities to help make copying data around into raw, possibly-uninitialized buffers easier and safer.**
11+
**Utilities to help make working with raw, possibly-uninitialized buffers easier and safer.**
1212

1313
[![Embark](https://img.shields.io/badge/embark-open%20source-blueviolet.svg)](https://embark.dev)
1414
[![Embark](https://img.shields.io/badge/discord-ark-%237289da.svg?logo=discord)](https://discord.gg/dAuKfZS)
@@ -18,7 +18,7 @@
1818
[![dependency status](https://deps.rs/repo/github/EmbarkStudios/presser/status.svg)](https://deps.rs/repo/github/EmbarkStudios/presser)
1919
</div>
2020

21-
`presser` can help you when copying data into raw buffers. One primary use-case is copying data into
21+
`presser` can help you when copying data into and reading data from raw buffers. One primary use-case is copying data into
2222
graphics-api-allocated buffers which will then be accessed by the GPU. Common methods for doing this
2323
right now in Rust can often invoke UB in subtle and hard-to-see ways. For example, viewing an allocated
2424
but uninitialized buffer as an `&mut [u8]` **is instantly undefined behavior**\*, and `transmute`ing even a
@@ -99,10 +99,20 @@ let copy_record = presser::copy_to_offset(&my_data, &mut slab, 0)?;
9999
// `my_data` may be placed at a different offset than requested. so, we check the returned
100100
// `CopyRecord` to check the actual start offset of the copied data.
101101
let actual_start_offset = copy_record.copy_start_offset;
102+
103+
// we may later (*unsafely*) read back our data. note that the read helpers provided by presser
104+
// are mostly unsafe. They do help protect you from some common footguns, but you still ultimately need
105+
// to guarantee you put the proper data where you're telling it you put the proper data.
106+
let my_copied_data_in_my_buffer: &MyDataStruct = unsafe {
107+
presser::read_at_offset(&slab, actual_start_offset)?
108+
};
102109
```
103110

104-
Note that actually accessing the copied data is a completely separate issue which `presser` does not
105-
(as of now) concern itself with. BE CAREFUL!
111+
Note that, as seen at the end, actually accessing the copied data is still unsafe. This means that you still need
112+
to take care that you're laying out your data exactly as whatever later reads it expects, whether that be a graphics
113+
API or your own data structure built on top of `presser`. The read functions that `presser` provides help check some
114+
common footguns (ensuring the given offset within the slab is properly aligned and the slab has enough memory to contain
115+
the wanted type), but they're still ultimately unsafe and require you to assert you put the proper data in the proper place.
106116

107117
See more in [the git `main` docs](https://embarkstudios.github.io/presser/presser/index.html)
108118
or [the released version docs](https://docs.rs/presser).

0 commit comments

Comments
 (0)