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
//! Async-aware Container Types
//!
//! These types play well with [MnemosAlloc][crate::heap::MnemosAlloc]
use core::{
alloc::Layout,
cell::UnsafeCell,
mem::MaybeUninit,
ops::{Deref, DerefMut},
ptr::NonNull,
};
use crate::heap::alloc;
//
// Arc
//
/// A wrapper of [`alloc::sync::Arc<T>`]
pub struct Arc<T: ?Sized> {
inner: alloc::sync::Arc<T>,
}
// These require the same bounds as `alloc::sync::Arc`'s `Send` and `Sync`
// impls.
unsafe impl<T: Send + Sync> Send for Arc<T> {}
unsafe impl<T: Send + Sync> Sync for Arc<T> {}
impl<T> Arc<T> {
/// Attempt to allocate a new reference counted T.
///
/// Returns an error containing the provided value if the allocation
/// could not immediately succeed.
///
/// NOTE/TODO: Today this will panic if not immediately successful. This should
/// be fixed in the future
pub fn try_new(t: T) -> Result<Self, T> {
Ok(Self {
inner: alloc::sync::Arc::new(t),
})
}
/// Attempt to allocate a new reference counted T.
///
/// Will not complete until the allocation succeeds
///
/// NOTE/TODO: Today this will panic if not immediately successful. This should
/// be fixed in the future
pub async fn new(t: T) -> Self {
Self {
inner: alloc::sync::Arc::new(t),
}
}
/// Convert into a pointer
///
/// This does NOT change the strong reference count
pub fn into_raw(a: Self) -> NonNull<T> {
unsafe { NonNull::new_unchecked(alloc::sync::Arc::into_raw(a.inner).cast_mut()) }
}
/// Restore from a pointer
///
/// This does NOT change the strong reference count.
///
/// # Safety
///
/// This has the same safety invariants as [alloc::sync::Arc].
#[inline(always)]
pub unsafe fn from_raw(nn: NonNull<T>) -> Self {
Self {
inner: alloc::sync::Arc::from_raw(nn.as_ptr()),
}
}
/// Increment the strong reference count
///
/// # Safety
///
/// This has the same afety invariants as [alloc::sync::Arc::increment_strong_count()].
#[inline(always)]
pub unsafe fn increment_strong_count(ptr: *const T) {
alloc::sync::Arc::increment_strong_count(ptr)
}
}
impl<T> Clone for Arc<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T> Deref for Arc<T> {
type Target = alloc::sync::Arc<T>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
//
// Box
//
/// A wrapper of [`alloc::boxed::Box<T>`]
pub struct Box<T> {
inner: alloc::boxed::Box<T>,
}
unsafe impl<T: Send> Send for Box<T> {}
unsafe impl<T: Sync> Sync for Box<T> {}
impl<T> Box<T> {
/// Attempt to allocate a new owned T.
///
/// Will not complete until the allocation succeeds.
pub async fn new(t: T) -> Self {
let ptr: *mut T = alloc(Layout::new::<T>()).await.cast().as_ptr();
unsafe {
ptr.write(t);
Self::from_raw(ptr)
}
}
/// Attempt to allocate a new owned T.
///
/// Returns an error containing the provided value if the allocation
/// could not immediately succeed.
pub fn try_new(t: T) -> Result<Self, T> {
match NonNull::new(unsafe { alloc::alloc::alloc(Layout::new::<T>()) }) {
Some(ptr) => unsafe {
let ptr = ptr.cast::<T>().as_ptr();
ptr.write(t);
Ok(Self {
inner: alloc::boxed::Box::from_raw(ptr),
})
},
None => Err(t),
}
}
/// Convert into a pointer
pub fn into_raw(me: Self) -> *mut T {
alloc::boxed::Box::into_raw(me.inner)
}
/// Convert from a pointer
///
/// # Safety
///
/// This has the same safety invariants as [alloc::boxed::Box::from_raw()]
pub unsafe fn from_raw(ptr: *mut T) -> Self {
Self {
inner: alloc::boxed::Box::from_raw(ptr),
}
}
/// Convert to a regular old alloc box
pub fn into_alloc_box(self) -> alloc::boxed::Box<T> {
self.inner
}
}
impl<T> Deref for Box<T> {
type Target = alloc::boxed::Box<T>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for Box<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
//
// ArrayBuf
//
/// A spooky owned array type
///
/// This type represents ownership of essentially an `UnsafeCell<MaybeUninit<[T]>>`.
///
/// It is intended as a low level building block for things like bbqueue and other data
/// structures that need to own a specific number of items, and would like to set their
/// own safety invariants, without manually using `alloc`.
pub struct ArrayBuf<T> {
ptr: NonNull<UnsafeCell<MaybeUninit<T>>>,
len: usize,
}
unsafe impl<T: Send> Send for ArrayBuf<T> {}
unsafe impl<T: Sync> Sync for ArrayBuf<T> {}
impl<T> ArrayBuf<T> {
/// Gets the layout for `len` items
///
/// Panics if creating the layout would fail (e.g. too large for the platform)
fn layout(len: usize) -> Layout {
Layout::array::<UnsafeCell<MaybeUninit<T>>>(len).unwrap()
}
/// Try to allocate a new ArrayBuf with storage for `len` items.
///
/// Returns None if the allocation does not succeed immediately.
///
/// Panics if the len is zero, or large enough that creating the layout would fail
pub fn try_new_uninit(len: usize) -> Option<Self> {
assert_ne!(len, 0, "ZST ArrayBuf doesn't make sense");
let layout = Self::layout(len);
let ptr = NonNull::new(unsafe { alloc::alloc::alloc(layout) })?.cast();
Some(ArrayBuf { ptr, len })
}
/// Try to allocate a new ArrayBuf with storage for `len` items.
///
/// Will not return until allocation succeeds.
///
/// Panics if the len is zero, or large enough that creating the layout would fail
pub async fn new_uninit(len: usize) -> Self {
assert_ne!(len, 0, "ZST ArrayBuf doesn't make sense");
let layout = Self::layout(len);
let ptr = alloc(layout).await.cast();
ArrayBuf { ptr, len }
}
/// Obtain a pointer to the heap allocated storage, as well as the length of items
///
/// This does NOT leak the heap allocation. The returned pointer has the lifetime
/// of this `ArrayBuf`.
pub fn ptrlen(&self) -> (NonNull<UnsafeCell<MaybeUninit<T>>>, usize) {
(self.ptr, self.len)
}
/// Returns the length of the `ArrayBuf`.
#[inline]
#[must_use]
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.len
}
}
impl<T> Drop for ArrayBuf<T> {
fn drop(&mut self) {
debug_assert_ne!(self.len, 0, "how did you do that");
let layout = Self::layout(self.len);
unsafe {
alloc::alloc::dealloc(self.ptr.as_ptr().cast(), layout);
}
}
}
impl<T> Deref for ArrayBuf<T> {
type Target = [UnsafeCell<MaybeUninit<T>>];
fn deref(&self) -> &Self::Target {
unsafe {
// Safety: the `ArrayBuf` logically owns `self.ptr`, and it is only
// deallocated when the `ArrayBuf` is dropped. The `ArrayBuf` was
// allocated with a layout of `self.len` `T`s, and thus the
// constructed slice should not exceed the bounds of the allocation.
core::slice::from_raw_parts(self.ptr.as_ptr(), self.len)
}
}
}
impl<T> DerefMut for ArrayBuf<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {
// Safety: the `ArrayBuf` logically owns `self.ptr`, and it is only
// deallocated when the `ArrayBuf` is dropped. The `ArrayBuf` was
// allocated with a layout of `self.len` `T`s, and thus the
// constructed slice should not exceed the bounds of the allocation.
core::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len)
}
}
}
//
// HeapArray
//
/// A heap allocation of a `[T; N]`. Useful for things like buffers that never need to
/// change size (unlike [FixedVec]), and are less spooky than [ArrayBuf].
pub struct HeapArray<T> {
ptr: NonNull<T>,
len: usize,
}
unsafe impl<T: Send> Send for HeapArray<T> {}
unsafe impl<T: Sync> Sync for HeapArray<T> {}
impl<T> HeapArray<T> {
/// Gets the layout for `len` items
///
/// Panics if creating the layout would fail (e.g. too large for the platform)
fn layout(len: usize) -> Layout {
Layout::array::<T>(len).unwrap()
}
/// Try to allocate a new HeapArray with storage for `len` items.
///
/// Will not return until allocation succeeds.
///
/// Panics if the len is zero, or large enough that creating the layout would fail
pub async fn new(len: usize, init: T) -> Self
where
T: Copy,
{
assert_ne!(len, 0, "ZST HeapArray doesn't make sense");
let layout = Self::layout(len);
let ptr: NonNull<T> = alloc(layout).await.cast();
unsafe {
let ptr = ptr.as_ptr();
for i in 0..len {
ptr.add(i).write(init);
}
}
HeapArray { ptr, len }
}
/// Returns the length of the `HeapArray`.
#[inline]
#[must_use]
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.len
}
}
impl<T> Drop for HeapArray<T> {
fn drop(&mut self) {
debug_assert_ne!(self.len, 0, "how did you do that");
let layout = Self::layout(self.len);
unsafe {
alloc::alloc::dealloc(self.ptr.as_ptr().cast(), layout);
}
}
}
impl<T> Deref for HeapArray<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
unsafe {
// Safety: the `HeapArray` logically owns `self.ptr`, and it is only
// deallocated when the `HeapArray` is dropped. The `HeapArray` was
// allocated with a layout of `self.len` `T`s, and thus the
// constructed slice should not exceed the bounds of the allocation.
core::slice::from_raw_parts(self.ptr.as_ptr(), self.len)
}
}
}
impl<T> DerefMut for HeapArray<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {
// Safety: the `ArrayBuf` logically owns `self.ptr`, and it is only
// deallocated when the `ArrayBuf` is dropped. The `ArrayBuf` was
// allocated with a layout of `self.len` `T`s, and thus the
// constructed slice should not exceed the bounds of the allocation.
core::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len)
}
}
}
//
// FixedVec
//
/// A `Vec` with a fixed upper size
///
/// Semantically, [FixedVec] works basically the same as [alloc::vec::Vec], however
/// [FixedVec] will NOT ever reallocate to increase size. In practice, this acts like
/// a heap allocated version of heapless' Vec type.
pub struct FixedVec<T> {
inner: alloc::vec::Vec<T>,
}
unsafe impl<T: Send> Send for FixedVec<T> {}
unsafe impl<T: Sync> Sync for FixedVec<T> {}
impl<T> FixedVec<T> {
/// Try to allocate a new FixedVec with storage for UP TO `capacity` items.
///
/// Returns None if the allocation does not succeed immediately.
///
/// Panics if the len is zero, or large enough that creating the layout would fail
pub fn try_new(capacity: usize) -> Option<Self> {
assert_ne!(capacity, 0, "ZST FixedVec doesn't make sense");
let layout = Layout::array::<T>(capacity).unwrap();
unsafe {
let ptr = NonNull::new(alloc::alloc::alloc(layout))?;
Some(FixedVec {
inner: alloc::vec::Vec::from_raw_parts(ptr.cast().as_ptr(), 0, capacity),
})
}
}
/// Try to allocate a new FixedVec with storage for UP TO `capacity` items.
///
/// Will not return until allocation succeeds.
///
/// Panics if the len is zero, or large enough that creating the layout would fail
pub async fn new(capacity: usize) -> Self {
assert_ne!(capacity, 0, "ZST FixedVec doesn't make sense");
let layout = Layout::array::<T>(capacity).unwrap();
unsafe {
let ptr = alloc(layout).await;
FixedVec {
inner: alloc::vec::Vec::from_raw_parts(ptr.cast().as_ptr(), 0, capacity),
}
}
}
/// Attempt to push an item into the fixed vec.
///
/// Returns an error if the fixed vec is full
#[inline]
pub fn try_push(&mut self, t: T) -> Result<(), T> {
if self.is_full() {
Err(t)
} else {
self.inner.push(t);
Ok(())
}
}
/// Removes the last element from a vector and returns it, or [`None`] if it
/// is empty.
///
/// This method is identical to the [`Vec::pop`](alloc::vec::Vec::pop)
/// method in `liballoc`.
#[inline]
pub fn pop(&mut self) -> Option<T> {
self.inner.pop()
}
/// Attempt to push an item into the fixed vec.
///
/// Returns an error if the slice would not fit in the capacity.
/// If an error is returned, the contents of the FixedVec is unchanged
#[inline]
#[allow(clippy::result_unit_err)]
pub fn try_extend_from_slice(&mut self, sli: &[T]) -> Result<(), ()>
where
T: Clone,
{
let new_len = match self.inner.len().checked_add(sli.len()) {
Some(c) => c,
None => return Err(()),
};
if new_len > self.inner.capacity() {
return Err(());
}
self.inner.extend_from_slice(sli);
Ok(())
}
/// Obtain a reference to the underlying [alloc::vec::Vec]
#[inline]
pub fn as_vec(&self) -> &alloc::vec::Vec<T> {
&self.inner
}
/// Get inner mutable vec
///
/// # Safety
///
/// You must not do anything that could realloc or increase the capacity.
/// We want an exact upper limit.
///
/// This would not be memory unsafe, but would violate the invariants of [FixedVec],
/// which is supposed to have a fixed upper size.
#[inline]
pub unsafe fn as_vec_mut(&mut self) -> &mut alloc::vec::Vec<T> {
&mut self.inner
}
/// Retains only the elements specified by the predicate.
///
/// In other words, remove all elements `e` for which `f(&e)` returns `false`.
/// This method operates in place, visiting each element exactly once in the
/// original order, and preserves the order of the retained elements.
///
/// This method is identical to the
/// [`Vec::retain`](alloc::vec::Vec::retain) method in `liballoc`.
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&T) -> bool,
{
self.inner.retain(f)
}
/// Retains only the elements specified by the predicate, passing a mutable reference to it.
///
/// In other words, remove all elements `e` such that `f(&mut e)` returns `false`.
/// This method operates in place, visiting each element exactly once in the
/// original order, and preserves the order of the retained elements.
///
/// This method is identical to the
/// [`Vec::retain_mut`](alloc::vec::Vec::retain_mut) method in `liballoc`.
pub fn retain_mut<F>(&mut self, f: F)
where
F: FnMut(&mut T) -> bool,
{
self.inner.retain_mut(f)
}
/// Obtain a reference to the current contents
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.inner
}
/// Obtain a mutable reference to the current contents
#[inline]
pub fn as_slice_mut(&mut self) -> &mut [T] {
&mut self.inner
}
/// Clear the FixedVec
///
/// This method is identical to the [`Vec::clear`](alloc::vec::Vec::clear)
/// method in `liballoc`.
#[inline]
pub fn clear(&mut self) {
self.inner.clear();
}
/// Is the FixedVec full?
#[inline]
pub fn is_full(&self) -> bool {
self.inner.len() == self.inner.capacity()
}
/// Returns `true` if this `FixedVec` is empty (its [`len`](Self::len) is
/// 0).
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Returns the length of the `FixedVec`.
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.inner.len()
}
/// Returns the total capacity in this `FixedVec`.
///
/// This method is identical to the
/// [`Vec::capacity`](alloc::vec::Vec::capacity) method in `liballoc`.
#[inline]
#[must_use]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
}
impl<T> AsRef<[T]> for FixedVec<T> {
#[inline(always)]
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}
impl<T> AsMut<[T]> for FixedVec<T> {
#[inline(always)]
fn as_mut(&mut self) -> &mut [T] {
self.as_slice_mut()
}
}