25
25
#include " subspace/containers/__private/slice_iter.h"
26
26
#include " subspace/fn/callable.h"
27
27
#include " subspace/iter/iterator_defn.h"
28
+ #include " subspace/macros/lifetimebound.h"
28
29
#include " subspace/marker/unsafe.h"
29
30
#include " subspace/mem/clone.h"
30
31
#include " subspace/num/unsigned_integer.h"
@@ -67,7 +68,7 @@ class [[sus_trivial_abi]] Slice {
67
68
// sus::construct::From<Slice<T>, T[]> trait.
68
69
template <size_t N>
69
70
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 ) {
71
72
return Slice (data, N);
72
73
}
73
74
@@ -82,12 +83,13 @@ class [[sus_trivial_abi]] Slice {
82
83
// / # Panics
83
84
// / If the index `i` is beyond the end of the slice, the function will panic.
84
85
// / #[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 {
86
88
check (i < len_);
87
89
return data_[i.primitive_value ];
88
90
}
89
91
90
- constexpr T& operator [](usize i) & noexcept
92
+ constexpr T& operator [](usize i) & noexcept sus_lifetimebound
91
93
requires (!std::is_const_v<T>)
92
94
{
93
95
check (i < len_);
@@ -96,7 +98,7 @@ class [[sus_trivial_abi]] Slice {
96
98
97
99
// / Returns a const reference to the element at index `i`, or `None` if
98
100
// / `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 {
100
102
if (i < len_) [[likely]]
101
103
return Option<const T&>::some (data_[i.primitive_value ]);
102
104
else
@@ -105,7 +107,7 @@ class [[sus_trivial_abi]] Slice {
105
107
106
108
// / Returns a mutable reference to the element at index `i`, or `None` if
107
109
// / `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
109
111
requires (!std::is_const_v<T>)
110
112
{
111
113
if (i < len_) [[likely]]
@@ -121,7 +123,8 @@ class [[sus_trivial_abi]] Slice {
121
123
// / Behaviour results. The size of the slice must therefore also have a length
122
124
// / of at least 1.
123
125
constexpr inline const T& get_unchecked (::sus::marker::UnsafeFnMarker,
124
- usize i) const & noexcept {
126
+ usize i) const & noexcept
127
+ sus_lifetimebound {
125
128
return data_[i.primitive_value ];
126
129
}
127
130
@@ -132,7 +135,7 @@ class [[sus_trivial_abi]] Slice {
132
135
// / Behaviour results. The size of the slice must therefore also have a length
133
136
// / of at least 1.
134
137
constexpr inline T& get_unchecked_mut (::sus::marker::UnsafeFnMarker,
135
- usize i) & noexcept
138
+ usize i) & noexcept sus_lifetimebound
136
139
requires (!std::is_const_v<T>)
137
140
{
138
141
return data_[i.primitive_value ];
@@ -151,7 +154,8 @@ class [[sus_trivial_abi]] Slice {
151
154
// / function will panic.
152
155
// / #[doc.overloads=slice.index.range]
153
156
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 {
155
159
const usize start = range.start_bound ().unwrap_or (0u );
156
160
const usize end = range.end_bound ().unwrap_or (len_);
157
161
const usize len = end >= start ? end - start : 0u ;
@@ -172,7 +176,8 @@ class [[sus_trivial_abi]] Slice {
172
176
// / Returns None if the Range would otherwise contain an element that is out
173
177
// / of bounds.
174
178
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 {
176
181
const usize start = range.start_bound ().unwrap_or (0u );
177
182
const usize end = range.end_bound ().unwrap_or (len_);
178
183
const usize len = end >= start ? end - start : 0u ;
@@ -195,7 +200,8 @@ class [[sus_trivial_abi]] Slice {
195
200
// / of bounds of the Slice, which can result in Undefined Behaviour.
196
201
constexpr Slice<T> get_range_unchecked (
197
202
::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 {
199
205
const usize start = range.start_bound ().unwrap_or (0u );
200
206
const usize end = range.end_bound ().unwrap_or (len_);
201
207
const usize len = end >= start ? end - start : 0u ;
@@ -278,13 +284,13 @@ class [[sus_trivial_abi]] Slice {
278
284
}
279
285
280
286
// / 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 {
282
288
check (len_ > 0_usize);
283
289
return data_;
284
290
}
285
291
286
292
// / 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
288
294
requires (!std::is_const_v<T>)
289
295
{
290
296
check (len_ > 0_usize);
@@ -294,14 +300,14 @@ class [[sus_trivial_abi]] Slice {
294
300
// / Returns an iterator over all the elements in the slice, visited in the
295
301
// / same order they appear in the slice. The iterator gives const access to
296
302
// / each element.
297
- constexpr SliceIter<const T&> iter () const & noexcept {
303
+ constexpr SliceIter<const T&> iter () const & noexcept sus_lifetimebound {
298
304
return SliceIter<const T&>::with (data_, len_);
299
305
}
300
306
301
307
// / Returns an iterator over all the elements in the slice, visited in the
302
308
// / same order they appear in the slice. The iterator gives mutable access to
303
309
// / each element.
304
- constexpr SliceIterMut<T&> iter_mut () noexcept
310
+ constexpr SliceIterMut<T&> iter_mut () noexcept sus_lifetimebound
305
311
requires (!std::is_const_v<T>)
306
312
{
307
313
return SliceIterMut<T&>::with (data_, len_);
@@ -323,11 +329,13 @@ class [[sus_trivial_abi]] Slice {
323
329
return SliceIterMut<T&>::with (data_, len_);
324
330
}
325
331
332
+ // / Constructs a `Vec<T>` by cloning each value in the Slice.
326
333
Vec<std::remove_const_t <T>> to_vec () const &
327
334
requires (::sus::mem::Clone<T>);
328
335
329
336
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) {}
331
339
332
340
T* data_;
333
341
::sus::usize len_;
0 commit comments