|
8 | 8 |
|
9 | 9 | # `🗜 presser` |
10 | 10 |
|
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.** |
12 | 12 |
|
13 | 13 | [](https://embark.dev) |
14 | 14 | [](https://discord.gg/dAuKfZS) |
|
18 | 18 | [](https://deps.rs/repo/github/EmbarkStudios/presser) |
19 | 19 | </div> |
20 | 20 |
|
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 |
22 | 22 | graphics-api-allocated buffers which will then be accessed by the GPU. Common methods for doing this |
23 | 23 | right now in Rust can often invoke UB in subtle and hard-to-see ways. For example, viewing an allocated |
24 | 24 | 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)?; |
99 | 99 | // `my_data` may be placed at a different offset than requested. so, we check the returned |
100 | 100 | // `CopyRecord` to check the actual start offset of the copied data. |
101 | 101 | 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 | +}; |
102 | 109 | ``` |
103 | 110 |
|
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. |
106 | 116 |
|
107 | 117 | See more in [the git `main` docs](https://embarkstudios.github.io/presser/presser/index.html) |
108 | 118 | or [the released version docs](https://docs.rs/presser). |
|
0 commit comments