1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
#![allow(clippy::needless_lifetimes)] // important to be explicit here imo
use super::*;
/// Helper to read back data from an ffi function which expects a pointer into which it will write
/// a `T`.
///
/// `fill_slab` is a function in which you must guarantee to write a valid `T` at the given
/// [`*mut c_void`](c_void) pointer.
///
/// `slab` will be used as the backing data to write the `T` into. The `*mut c_void` pointer given
/// to the function will be as close to the beginning of `slab` as possible while upholding the
/// alignment requirements of `T`. If a `T` cannot fit into `slab` while upholding those alignment
/// requirements and the size of `T`, an error will be returned and `fill_slab` will not be called.
///
/// # Safety
///
/// You must during the execution of `fill_slab` **fully-initialize** a **valid**\* `T`
/// at the given pointer.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
pub unsafe fn readback_from_ffi<'a, T, S, F>(slab: &'a mut S, fill_slab: F) -> Result<&'a T, Error>
where
S: Slab + ?Sized,
F: FnOnce(*mut c_void),
{
let t_layout = Layout::new::<T>();
let offsets = compute_and_validate_offsets(slab, 0, t_layout, 1, false)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr_mut().add(offsets.start) }.cast::<c_void>();
fill_slab(ptr);
let ptr = ptr.cast::<T>().cast_const();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for `T` at `ptr`, checked by us
// - if the function-level safety guarantees are met, then:
// - `ptr` contains a previously-placed `T`
// - we have mutable access to all of `slab`, which includes `ptr`.
Ok(unsafe { &*ptr })
}
/// Helper to read back data from an ffi function which expects a pointer into which it will write
/// a slice (in C language, an array) of `T`s.
///
/// `fill_slab` is a function which takes as parameters first an aligned (for T)
/// [`*mut c_void`](c_void) and second the number of bytes left in `slab` available for writing.
/// It must then write a slice of `T`s into the given pointer and return the length, in units of
/// `T`, of the slice it wrote.
///
/// `slab` will be used as the backing data to write the slice of `T`s into. The `*mut c_void`
/// pointer given to the function will be as close to the beginning of `slab` as possible while
/// upholding the alignment requirements of `T`.
///
/// # Safety
///
/// You must during the execution of `fill_slab` **fully-initialize** a **valid**\* slice of `T`
/// beginning at the given pointer and with length greater than or equal to the length you return
/// from that function.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
pub unsafe fn readback_slice_from_ffi<'a, T, S, F>(
slab: &'a mut S,
fill_slab: F,
) -> Result<&'a [T], Error>
where
S: Slab + ?Sized,
F: FnOnce(*mut c_void, usize) -> usize,
{
let t_layout = Layout::new::<T>();
let offsets = compute_and_validate_offsets(slab, 0, t_layout, 1, false)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr_mut().add(offsets.start) }.cast::<c_void>();
let writable_size = slab.size() - offsets.end_padded;
let written_n_of_ts = fill_slab(ptr, writable_size);
let layout_claimed_written = Layout::array::<T>(written_n_of_ts)?;
let end_offset = offsets.start + layout_claimed_written.size();
if end_offset > slab.size() {
return Err(Error::OutOfMemory);
}
let ptr = ptr.cast::<T>().cast_const();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for `[T; written_n_of_ts]` at `ptr`, checked by us
// - if the function-level safety guarantees are met, then:
// - `ptr` contains a previously-placed `T`
// - we have mutable access to all of `slab`, which includes `ptr`.
Ok(unsafe { core::slice::from_raw_parts(ptr, written_n_of_ts) })
}
/// Gets a shared reference to a `T` within `slab` at `offset`.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `T` is placed.
///
/// The function will return an error if:
/// - `offset` within `slab` is not properly aligned for `T`
/// - `offset` is out of bounds of the `slab`
/// - `offset + size_of::<T>` is out of bounds of the `slab`
///
/// # Safety
///
/// You must have previously **fully-initialized** a **valid**\* `T` at the given offset into `slab`.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn read_at_offset<'a, T, S: Slab + ?Sized>(
slab: &'a S,
offset: usize,
) -> Result<&'a T, Error> {
let t_layout = Layout::new::<T>();
let offsets = compute_and_validate_offsets(slab, offset, t_layout, 1, true)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr().add(offsets.start) }.cast::<T>();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for `T` at `ptr`, checked by
// - if the function-level safety guarantees are met, then:
// - `ptr` contains a previously-placed `T`
// - we have shared access to all of `slab`, which includes `ptr`.
Ok(unsafe { &*ptr })
}
/// Gets a shared reference to a `T` within `slab` at `offset`, not checking any requirements.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `T` is placed.
///
/// # Safety
///
/// You must ensure:
///
/// - `offset` within `slab` is properly aligned for `T`
/// - `offset` is within bounds of the `slab`
/// - `offset + size_of::<T>` is within bounds of the `slab`
/// - You must have previously **fully-initialized** a **valid**\* `T` at the given offset into `slab`.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn read_at_offset_unchecked<'a, T, S: Slab + ?Sized>(
slab: &'a S,
offset: usize,
) -> &'a T {
// SAFETY: if offset is within the slab as guaranteed by function-level safety, this is
// safe since a slab's size must be < isize::MAX
let ptr = unsafe { slab.base_ptr().add(offset) }.cast::<T>();
// SAFETY:
// - we have shared access to all of `slab`, which includes `ptr`.
// - if the function-level safety guarantees are met, then:
// - `ptr` is properly aligned
// - `slab` contains enough space for `T` at `ptr`
// - `ptr` contains a previously-placed `T`
unsafe { &*ptr }
}
/// Gets a mutable reference to a `T` within `slab` at `offset`.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `T` is placed.
///
/// The function will return an error if:
/// - `offset` within `slab` is not properly aligned for `T`
/// - `offset` is out of bounds of the `slab`
/// - `offset + size_of::<T>` is out of bounds of the `slab`
///
/// # Safety
///
/// You must have previously **fully-initialized** a **valid**\* `T` at the given offset into `slab`. If you want to fill an uninitialized
/// buffer with data, you should instead use any of the copy helper functions or one of the `maybe_uninit_mut` read functions.
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn read_at_offset_mut<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
) -> Result<&'a mut T, Error> {
let t_layout = Layout::new::<T>();
let offsets = compute_and_validate_offsets(slab, offset, t_layout, 1, true)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr_mut().add(offsets.start) }.cast::<T>();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for `T` at `ptr`, checked by us
// - we have unique access to all of `slab`, which includes `ptr`.
// - if the function-level safety guarantees are met, then:
// - `ptr` contains a previously-placed `T`
Ok(unsafe { &mut *ptr })
}
/// Gets a mutable reference to a `T` within `slab` at `offset`, not checking any requirements.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `T` is placed.
///
/// # Safety
///
/// You must ensure:
///
/// - `offset` within `slab` is properly aligned for `T`
/// - `offset` is within bounds of the `slab`
/// - `offset + size_of::<T>` is within bounds of the `slab`
/// - You must have previously **fully-initialized** a **valid** `T` at the given offset into `slab`. If you want to fill an uninitialized
/// buffer with data, you should instead use any of the copy helper functions or one of the `maybe_uninit_mut` read functions.
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn read_at_offset_mut_unchecked<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
) -> &'a mut T {
// SAFETY: if offset is within the slab as guaranteed by function-level safety, this is
// safe since a slab's size must be < isize::MAX
let ptr = unsafe { slab.base_ptr_mut().add(offset) }.cast::<T>();
// SAFETY:
// - we have mutable access to all of `slab`, which includes `ptr`.
// - if the function-level safety guarantees are met, then:
// - `ptr` is properly aligned
// - `slab` contains enough space for `T` at `ptr`
// - `ptr` contains a previously-placed `T`
unsafe { &mut *ptr }
}
/// Gets a mutable reference to a `MaybeUninit<T>` within `slab` at `offset`.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `T` may be placed.
///
/// The function will return an error if:
/// - `offset` within `slab` is not properly aligned for `T`
/// - `offset` is out of bounds of the `slab`
/// - `offset + size_of::<T>` is out of bounds of the `slab`
///
/// # Safety
///
/// This function is safe since in order to read any data you need to call the unsafe [`MaybeUninit::assume_init`] on the returned value.
/// However, you should know that if you do that, you must have ensured that there is indeed a **valid**\* `T` in its place.
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub fn get_maybe_uninit_at_offset_mut<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
) -> Result<&'a mut MaybeUninit<T>, Error> {
let t_layout = Layout::new::<T>();
let offsets = compute_and_validate_offsets(slab, offset, t_layout, 1, true)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr_mut().add(offsets.start) }.cast::<MaybeUninit<T>>();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for `T` at `ptr`, checked by us
// - if the function-level safety guarantees are met, then:
// - we have unique access to all of `slab`, which includes `ptr`.
Ok(unsafe { &mut *ptr })
}
/// Gets a mutable reference to a `MaybeUninit<T>` within `slab` at `offset`, not checking any requirements.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `T` is placed.
///
/// # Safety
///
/// You must ensure:
///
/// - `offset` within `slab` is properly aligned for `T`
/// - `offset` is within bounds of the `slab`
/// - `offset + size_of::<T>` is within bounds of the `slab`
///
/// You must have ensured there is a **fully-initialized** and **valid**\* `T` at the given offset into `slab` before calling [`MaybeUninit::assume_init`].
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn get_maybe_uninit_at_offset_mut_unchecked<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
) -> &'a mut MaybeUninit<T> {
// SAFETY: if offset is within the slab as guaranteed by function-level safety, this is
// safe since a slab's size must be < isize::MAX
let ptr = unsafe { slab.base_ptr_mut().add(offset) }.cast::<MaybeUninit<T>>();
// SAFETY:
// - we have mutable access to all of `slab`, which includes `ptr`.
// - if the function-level safety guarantees are met, then:
// - `ptr` is properly aligned
// - `slab` contains enough space for `T` at `ptr`
unsafe { &mut *ptr }
}
/// Reads a `&[T]` within `slab` at `offset`.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `[T; len]` is placed.
/// - `len` is the length of the returned slice, counted in elements of `T`.
///
/// The function will return an error if:
/// - `offset` within `slab` is not properly aligned for `T`
/// - `offset` is out of bounds of the `slab`
/// - `offset + size_of::<T> * len` is out of bounds of the `slab`
///
/// # Safety
///
/// You must have previously **fully-initialized** a **valid**\* a `[T; len]` at the given offset into `slab`.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn read_slice_at_offset<'a, T, S: Slab + ?Sized>(
slab: &'a S,
offset: usize,
len: usize,
) -> Result<&'a [T], Error> {
let t_layout = match Layout::array::<T>(len) {
Ok(layout) => layout,
Err(_) => return Err(Error::InvalidLayout),
};
let offsets = compute_and_validate_offsets(slab, offset, t_layout, 1, true)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr().add(offsets.start) }.cast::<T>();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for the slice's layout, checked by us
// - if the function-level safety guarantees are met, then:
// - `ptr` contains a previously-placed `[T; len]`
// - we have shared access to all of `slab`, which includes `ptr`.
Ok(unsafe { core::slice::from_raw_parts(ptr, len) })
}
/// Reads a `&[T]` within `slab` at `offset`, not checking any requirements.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `[T; len]` is placed.
/// - `len` is the length of the returned slice, counted in elements of `T`.
///
/// # Safety
///
/// You must ensure:
///
/// - `offset` within `slab` is properly aligned for `T`
/// - `offset` is within bounds of the `slab`
/// - `offset + size_of::<T> * len` is within bounds of the `slab`
/// - You must have previously **fully-initialized** a **valid**\* a `[T; len]` at the given offset into `slab`.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
/// - See also safety docs of [`core::slice::from_raw_parts`].
#[inline]
pub unsafe fn read_slice_at_offset_unchecked<'a, T, S: Slab + ?Sized>(
slab: &'a S,
offset: usize,
len: usize,
) -> &'a [T] {
// SAFETY: if offset is within the slab as guaranteed by function-level safety, this is
// safe since a slab's size must be < isize::MAX
let ptr = unsafe { slab.base_ptr().add(offset) }.cast::<T>();
// SAFETY:
// - we have shared access to all of `slab`, which includes `ptr`.
// - if the function-level safety guarantees are met, then:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for the slice's layout, checked by us
// - `ptr` contains a previously-placed `[T; len]`
unsafe { core::slice::from_raw_parts(ptr, len) }
}
/// Reads a `&mut [T]` within `slab` at `offset`.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `[T; len]` is placed.
/// - `len` is the length of the returned slice, counted in elements of `T`.
///
/// The function will return an error if:
/// - `offset` within `slab` is not properly aligned for `T`
/// - `offset` is out of bounds of the `slab`
/// - `offset + size_of::<T> * len` is out of bounds of the `slab`
///
/// # Safety
///
/// You must have previously **fully-initialized** a **valid**\* `[T; len]` at the given offset into `slab`. If you want to fill an uninitialized
/// buffer with data, you should instead use any of the copy helper functions or one of the `maybe_uninit_mut` read functions.
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn read_slice_at_offset_mut<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
len: usize,
) -> Result<&'a mut [T], Error> {
let t_layout = match Layout::array::<T>(len) {
Ok(layout) => layout,
Err(_) => return Err(Error::InvalidLayout),
};
let offsets = compute_and_validate_offsets(slab, offset, t_layout, 1, true)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr_mut().add(offsets.start) }.cast::<T>();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for the slice's layout, checked by us
// - if the function-level safety guarantees are met, then:
// - `ptr` contains a previously-placed `[T; len]`
// - we have mutable access to all of `slab`, which includes `ptr`.
Ok(unsafe { core::slice::from_raw_parts_mut(ptr, len) })
}
/// Reads a `&mut [T]` within `slab` at `offset`, not checking any requirements.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `[T; len]` is placed.
/// - `len` is the length of the returned slice, counted in elements of `T`.
///
/// # Safety
///
/// You must ensure:
///
/// - `offset` within `slab` is properly aligned for `T`
/// - `offset` is within bounds of the `slab`
/// - `offset + size_of::<T> * len` is within bounds of the `slab`
/// - You must have previously **fully-initialized** a **valid** a `[T; len]` at the given offset into `slab`. If you want to fill an uninitialized
/// buffer with data, you should instead use any of the copy helper functions or one of the `maybe_uninit_mut` read functions.
/// - See also safety docs of [`core::slice::from_raw_parts_mut`].
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn read_slice_at_offset_mut_unchecked<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
len: usize,
) -> &'a mut [T] {
// SAFETY: if offset is within the slab as guaranteed by function-level safety, this is
// safe since a slab's size must be < isize::MAX
let ptr = unsafe { slab.base_ptr_mut().add(offset) }.cast::<T>();
// SAFETY:
// - we have shared access to all of `slab`, which includes `ptr`.
// - if the function-level safety guarantees are met, then:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for the slice's layout, checked by us
// - `ptr` contains a previously-placed `[T; len]`
unsafe { core::slice::from_raw_parts_mut(ptr, len) }
}
/// Gets a `&mut [MaybeUninit<T>]` within `slab` at `offset`.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `[T; len]` may be placed.
/// - `len` is the length of the returned slice, counted in elements of `T`.
///
/// The function will return an error if:
/// - `offset` within `slab` is not properly aligned for `T`
/// - `offset` is out of bounds of the `slab`
/// - `offset + size_of::<T> * len` is out of bounds of the `slab`
///
/// # Safety
///
/// This function is safe since in order to read any data you need to call the unsafe [`MaybeUninit::assume_init`] on the returned value.
/// However, you should know that if you do that, you must have ensured that there is indeed a **valid**\* `T` in its place.
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub fn get_maybe_uninit_slice_at_offset_mut<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
len: usize,
) -> Result<&'a mut [MaybeUninit<T>], Error> {
let t_layout = match Layout::array::<T>(len) {
Ok(layout) => layout,
Err(_) => return Err(Error::InvalidLayout),
};
let offsets = compute_and_validate_offsets(slab, offset, t_layout, 1, true)?;
// SAFETY: if compute_offsets succeeded, this has already been checked to be safe.
let ptr = unsafe { slab.base_ptr_mut().add(offsets.start) }.cast::<MaybeUninit<T>>();
// SAFETY:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for the slice's layout, checked by us
// - if the function-level safety guarantees are met, then:
// - we have mutable access to all of `slab`, which includes `ptr`.
Ok(unsafe { core::slice::from_raw_parts_mut(ptr, len) })
}
/// Gets a `&mut [MaybeUninit<T>]` within `slab` at `offset`, not checking any requirements.
///
/// - `offset` is the offset, in bytes, after the start of `slab` at which a `[T; len]` is placed.
/// - `len` is the length of the returned slice, counted in elements of `T`.
///
/// # Safety
///
/// You must ensure:
///
/// - `offset` within `slab` is properly aligned for `T`
/// - `offset` is within bounds of the `slab`
/// - `offset + size_of::<T> * len` is within bounds of the `slab`
/// - See also safety docs of [`core::slice::from_raw_parts_mut`].
///
/// You must have ensured there is a **fully-initialized** and **valid**\* `T` in each returned `MaybeUninit<T>` before calling [`MaybeUninit::assume_init`].
///
/// **Note that *if you write through the returned reference***, any *padding bytes* within the layout of `T`
/// (which for a `repr(Rust)` type is arbitrary and unknown) must thereafter be considered *uninitialized*
/// until you explicitly initialize them again. This means that if you write a `T` which contains
/// padding into `slab`, you **must not**, for example, try to read those bytes as `&[u8]` afterwards
/// (or as some other type which expects those bytes to be initialized), as you would then be
/// reading uninitialized memory, which is *undefined behavior*.
///
/// \* Validity is a complex topic not to be taken lightly.
/// See [this rust reference page](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for more details.
#[inline]
pub unsafe fn get_maybe_uninit_slice_at_offset_mut_unchecked<'a, T, S: Slab + ?Sized>(
slab: &'a mut S,
offset: usize,
len: usize,
) -> &'a mut [MaybeUninit<T>] {
// SAFETY: if offset is within the slab as guaranteed by function-level safety, this is
// safe since a slab's size must be < isize::MAX
let ptr = unsafe { slab.base_ptr_mut().add(offset) }.cast::<MaybeUninit<T>>();
// SAFETY:
// - we have shared access to all of `slab`, which includes `ptr`.
// - if the function-level safety guarantees are met, then:
// - `ptr` is properly aligned, checked by us
// - `slab` contains enough space for the slice's layout, checked by us
unsafe { core::slice::from_raw_parts_mut(ptr, len) }
}