26
26
#include " subspace/iter/from_iterator.h"
27
27
#include " subspace/iter/into_iterator.h"
28
28
#include " subspace/macros/compiler.h"
29
+ #include " subspace/macros/lifetimebound.h"
29
30
#include " subspace/mem/move.h"
30
31
#include " subspace/mem/relocate.h"
31
32
#include " subspace/mem/replace.h"
@@ -93,6 +94,8 @@ class Vec {
93
94
// / This is highly unsafe, due to the number of invariants that aren’t
94
95
// / checked:
95
96
// /
97
+ // / * `ptr` must be heap allocated through malloc() (TODO: Want our own global
98
+ // / allocator API).
96
99
// / * `T` needs to have an alignment no more than what `ptr` was allocated
97
100
// / with.
98
101
// / * The size of `T` times the `capacity` (ie. the allocated size in bytes)
@@ -372,7 +375,7 @@ class Vec {
372
375
}
373
376
374
377
// / Returns a const reference to the element at index `i`.
375
- constexpr Option<const T&> get (usize i) const & noexcept {
378
+ constexpr Option<const T&> get (usize i) const & noexcept sus_lifetimebound {
376
379
check (!is_moved_from ());
377
380
if (i >= len_) [[unlikely]]
378
381
return Option<const T&>::none ();
@@ -381,7 +384,7 @@ class Vec {
381
384
constexpr Option<const T&> get (usize i) && = delete;
382
385
383
386
// / Returns a mutable reference to the element at index `i`.
384
- constexpr Option<T&> get_mut (usize i) & noexcept {
387
+ constexpr Option<T&> get_mut (usize i) & noexcept sus_lifetimebound {
385
388
check (!is_moved_from ());
386
389
if (i >= len_) [[unlikely]]
387
390
return Option<T&>::none ();
@@ -399,7 +402,8 @@ class Vec {
399
402
// / Additionally, the Vec must not be in a moved-from state or the pointer
400
403
// / will be invalid and Undefined Behaviour results.
401
404
constexpr inline const T& get_unchecked (::sus::marker::UnsafeFnMarker,
402
- usize i) const & noexcept {
405
+ usize i) const & noexcept
406
+ sus_lifetimebound {
403
407
return storage_[i.primitive_value ];
404
408
}
405
409
constexpr inline const T& get_unchecked (::sus::marker::UnsafeFnMarker,
@@ -411,7 +415,7 @@ class Vec {
411
415
// / The index `i` must be inside the bounds of the array or Undefined
412
416
// / Behaviour results.
413
417
constexpr inline T& get_unchecked_mut (::sus::marker::UnsafeFnMarker,
414
- usize i) & noexcept {
418
+ usize i) & noexcept sus_lifetimebound {
415
419
return storage_[i.primitive_value ];
416
420
}
417
421
@@ -420,14 +424,15 @@ class Vec {
420
424
template <::sus::num::SignedPrimitiveInteger I>
421
425
constexpr inline const T& operator [](I) const = delete ;
422
426
423
- constexpr inline const T& operator [](usize i) const & noexcept {
427
+ constexpr inline const T& operator [](usize i) const & noexcept
428
+ sus_lifetimebound {
424
429
check (!is_moved_from ());
425
430
check (i < len_);
426
431
return get_unchecked (::sus::marker::unsafe_fn, i);
427
432
}
428
433
constexpr inline const T& operator [](usize i) && = delete ;
429
434
430
- constexpr inline T& operator [](usize i) & noexcept {
435
+ constexpr inline T& operator [](usize i) & noexcept sus_lifetimebound {
431
436
check (!is_moved_from ());
432
437
check (i < len_);
433
438
return get_unchecked_mut (::sus::marker::unsafe_fn, i);
@@ -459,7 +464,7 @@ class Vec {
459
464
// /
460
465
// / # Panics
461
466
// / Panics if the vector's capacity is zero.
462
- inline const T* as_ptr () const & noexcept {
467
+ inline const T* as_ptr () const & noexcept sus_lifetimebound {
463
468
check (!is_moved_from ());
464
469
check (is_alloced ());
465
470
return storage_;
@@ -470,15 +475,15 @@ class Vec {
470
475
// /
471
476
// / # Panics
472
477
// / Panics if the vector's capacity is zero.
473
- inline T* as_mut_ptr () & noexcept {
478
+ inline T* as_mut_ptr () & noexcept sus_lifetimebound {
474
479
check (!is_moved_from ());
475
480
check (is_alloced ());
476
481
return storage_;
477
482
}
478
483
479
484
// Returns a slice that references all the elements of the vector as const
480
485
// references.
481
- constexpr Slice<const T> as_slice () const & noexcept {
486
+ constexpr Slice<const T> as_slice () const & noexcept sus_lifetimebound {
482
487
check (!is_moved_from ());
483
488
// SAFETY: The `len_` is the number of elements in the Vec, and the pointer
484
489
// is to the start of the Vec, so this Slice covers a valid range.
@@ -489,7 +494,7 @@ class Vec {
489
494
490
495
// Returns a slice that references all the elements of the vector as mutable
491
496
// references.
492
- constexpr Slice<T> as_mut_slice () & noexcept {
497
+ constexpr Slice<T> as_mut_slice () & noexcept sus_lifetimebound {
493
498
check (!is_moved_from ());
494
499
// SAFETY: The `len_` is the number of elements in the Vec, and the pointer
495
500
// is to the start of the Vec, so this Slice covers a valid range.
@@ -499,7 +504,7 @@ class Vec {
499
504
// / Returns an iterator over all the elements in the array, visited in the
500
505
// / same order they appear in the array. The iterator gives const access to
501
506
// / each element.
502
- constexpr SliceIter<const T&> iter () const & noexcept {
507
+ constexpr SliceIter<const T&> iter () const & noexcept sus_lifetimebound {
503
508
check (!is_moved_from ());
504
509
return SliceIter<const T&>::with (storage_, len_);
505
510
}
@@ -508,7 +513,7 @@ class Vec {
508
513
// / Returns an iterator over all the elements in the array, visited in the
509
514
// / same order they appear in the array. The iterator gives mutable access to
510
515
// / each element.
511
- constexpr SliceIterMut<T&> iter_mut () & noexcept {
516
+ constexpr SliceIterMut<T&> iter_mut () & noexcept sus_lifetimebound {
512
517
check (!is_moved_from ());
513
518
return SliceIterMut<T&>::with (storage_, len_);
514
519
}
0 commit comments