Skip to content

Commit 7a23bf6

Browse files
committed
Apply lifetimebound to the Slice API
1 parent 5a9df79 commit 7a23bf6

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

subspace/containers/slice.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "subspace/containers/__private/slice_iter.h"
2626
#include "subspace/fn/callable.h"
2727
#include "subspace/iter/iterator_defn.h"
28+
#include "subspace/macros/lifetimebound.h"
2829
#include "subspace/marker/unsafe.h"
2930
#include "subspace/mem/clone.h"
3031
#include "subspace/num/unsigned_integer.h"
@@ -67,7 +68,7 @@ class [[sus_trivial_abi]] Slice {
6768
// sus::construct::From<Slice<T>, T[]> trait.
6869
template <size_t N>
6970
requires(N <= size_t{PTRDIFF_MAX})
70-
static constexpr inline Slice from(T (&data)[N]) {
71+
static constexpr inline Slice from(T (&data)[N] sus_lifetimebound) {
7172
return Slice(data, N);
7273
}
7374

@@ -82,12 +83,13 @@ class [[sus_trivial_abi]] Slice {
8283
/// # Panics
8384
/// If the index `i` is beyond the end of the slice, the function will panic.
8485
/// #[doc.overloads=slice.index.usize]
85-
constexpr inline const T& operator[](usize i) const& noexcept {
86+
constexpr inline const T& operator[](usize i) const& noexcept
87+
sus_lifetimebound {
8688
check(i < len_);
8789
return data_[i.primitive_value];
8890
}
8991

90-
constexpr T& operator[](usize i) & noexcept
92+
constexpr T& operator[](usize i) & noexcept sus_lifetimebound
9193
requires(!std::is_const_v<T>)
9294
{
9395
check(i < len_);
@@ -96,7 +98,7 @@ class [[sus_trivial_abi]] Slice {
9698

9799
/// Returns a const reference to the element at index `i`, or `None` if
98100
/// `i` is beyond the end of the Slice.
99-
constexpr Option<const T&> get(usize i) const& noexcept {
101+
constexpr Option<const T&> get(usize i) const& noexcept sus_lifetimebound {
100102
if (i < len_) [[likely]]
101103
return Option<const T&>::some(data_[i.primitive_value]);
102104
else
@@ -105,7 +107,7 @@ class [[sus_trivial_abi]] Slice {
105107

106108
/// Returns a mutable reference to the element at index `i`, or `None` if
107109
/// `i` is beyond the end of the Slice.
108-
constexpr Option<T&> get_mut(usize i) & noexcept
110+
constexpr Option<T&> get_mut(usize i) & noexcept sus_lifetimebound
109111
requires(!std::is_const_v<T>)
110112
{
111113
if (i < len_) [[likely]]
@@ -121,7 +123,8 @@ class [[sus_trivial_abi]] Slice {
121123
/// Behaviour results. The size of the slice must therefore also have a length
122124
/// of at least 1.
123125
constexpr inline const T& get_unchecked(::sus::marker::UnsafeFnMarker,
124-
usize i) const& noexcept {
126+
usize i) const& noexcept
127+
sus_lifetimebound {
125128
return data_[i.primitive_value];
126129
}
127130

@@ -132,7 +135,7 @@ class [[sus_trivial_abi]] Slice {
132135
/// Behaviour results. The size of the slice must therefore also have a length
133136
/// of at least 1.
134137
constexpr inline T& get_unchecked_mut(::sus::marker::UnsafeFnMarker,
135-
usize i) & noexcept
138+
usize i) & noexcept sus_lifetimebound
136139
requires(!std::is_const_v<T>)
137140
{
138141
return data_[i.primitive_value];
@@ -151,7 +154,8 @@ class [[sus_trivial_abi]] Slice {
151154
/// function will panic.
152155
/// #[doc.overloads=slice.index.range]
153156
constexpr inline Slice<T> operator[](
154-
const ::sus::ops::RangeBounds<usize> auto range) const noexcept {
157+
const ::sus::ops::RangeBounds<usize> auto range) const noexcept
158+
sus_lifetimebound {
155159
const usize start = range.start_bound().unwrap_or(0u);
156160
const usize end = range.end_bound().unwrap_or(len_);
157161
const usize len = end >= start ? end - start : 0u;
@@ -172,7 +176,8 @@ class [[sus_trivial_abi]] Slice {
172176
/// Returns None if the Range would otherwise contain an element that is out
173177
/// of bounds.
174178
constexpr Option<Slice<T>> get_range(
175-
const ::sus::ops::RangeBounds<usize> auto range) const noexcept {
179+
const ::sus::ops::RangeBounds<usize> auto range) const noexcept
180+
sus_lifetimebound {
176181
const usize start = range.start_bound().unwrap_or(0u);
177182
const usize end = range.end_bound().unwrap_or(len_);
178183
const usize len = end >= start ? end - start : 0u;
@@ -195,7 +200,8 @@ class [[sus_trivial_abi]] Slice {
195200
/// of bounds of the Slice, which can result in Undefined Behaviour.
196201
constexpr Slice<T> get_range_unchecked(
197202
::sus::marker::UnsafeFnMarker,
198-
const ::sus::ops::RangeBounds<usize> auto range) const noexcept {
203+
const ::sus::ops::RangeBounds<usize> auto range) const noexcept
204+
sus_lifetimebound {
199205
const usize start = range.start_bound().unwrap_or(0u);
200206
const usize end = range.end_bound().unwrap_or(len_);
201207
const usize len = end >= start ? end - start : 0u;
@@ -278,13 +284,13 @@ class [[sus_trivial_abi]] Slice {
278284
}
279285

280286
/// Returns a const pointer to the first element in the slice.
281-
inline const T* as_ptr() const& noexcept {
287+
inline const T* as_ptr() const& noexcept sus_lifetimebound {
282288
check(len_ > 0_usize);
283289
return data_;
284290
}
285291

286292
/// Returns a mutable pointer to the first element in the slice.
287-
inline T* as_mut_ptr() & noexcept
293+
inline T* as_mut_ptr() & noexcept sus_lifetimebound
288294
requires(!std::is_const_v<T>)
289295
{
290296
check(len_ > 0_usize);
@@ -294,14 +300,14 @@ class [[sus_trivial_abi]] Slice {
294300
/// Returns an iterator over all the elements in the slice, visited in the
295301
/// same order they appear in the slice. The iterator gives const access to
296302
/// each element.
297-
constexpr SliceIter<const T&> iter() const& noexcept {
303+
constexpr SliceIter<const T&> iter() const& noexcept sus_lifetimebound {
298304
return SliceIter<const T&>::with(data_, len_);
299305
}
300306

301307
/// Returns an iterator over all the elements in the slice, visited in the
302308
/// same order they appear in the slice. The iterator gives mutable access to
303309
/// each element.
304-
constexpr SliceIterMut<T&> iter_mut() noexcept
310+
constexpr SliceIterMut<T&> iter_mut() noexcept sus_lifetimebound
305311
requires(!std::is_const_v<T>)
306312
{
307313
return SliceIterMut<T&>::with(data_, len_);
@@ -323,11 +329,13 @@ class [[sus_trivial_abi]] Slice {
323329
return SliceIterMut<T&>::with(data_, len_);
324330
}
325331

332+
/// Constructs a `Vec<T>` by cloning each value in the Slice.
326333
Vec<std::remove_const_t<T>> to_vec() const&
327334
requires(::sus::mem::Clone<T>);
328335

329336
private:
330-
constexpr Slice(T* data, usize len) noexcept : data_(data), len_(len) {}
337+
constexpr Slice(T* data sus_lifetimebound, usize len) noexcept
338+
: data_(data), len_(len) {}
331339

332340
T* data_;
333341
::sus::usize len_;

0 commit comments

Comments
 (0)