Skip to content

Commit 9be3a37

Browse files
committed
Remove now-pointless reinterpret_casts in Vec
1 parent 13a99f7 commit 9be3a37

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

subspace/containers/vec.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ class Vec {
111111
/// Constructs a vector by taking all the elements from the iterator.
112112
///
113113
/// sus::iter::FromIterator trait.
114-
static constexpr Vec from_iter(::sus::iter::IntoIterator<T> auto&& into_iter) noexcept
114+
static constexpr Vec from_iter(
115+
::sus::iter::IntoIterator<T> auto&& into_iter) noexcept
115116
requires(::sus::mem::Move<T> && !std::is_reference_v<T>)
116117
{
117118
auto&& iter = sus::move(into_iter).into_iter();
@@ -274,8 +275,8 @@ class Vec {
274275
} else {
275276
auto* const new_storage =
276277
static_cast<T*>(malloc(bytes.primitive_value));
277-
auto* old_t = reinterpret_cast<T*>(storage_);
278-
auto* new_t = reinterpret_cast<T*>(new_storage);
278+
T* old_t = storage_;
279+
T* new_t = new_storage;
279280
const size_t len = size_t{len_};
280281
for (auto i = size_t{0}; i < len; ++i) {
281282
new (new_t) T(::sus::move(*old_t));
@@ -341,7 +342,7 @@ class Vec {
341342
{
342343
check(!is_moved_from());
343344
reserve(1_usize);
344-
new (as_mut_ptr() + size_t{len_}) T(::sus::move(t));
345+
new (&storage_[size_t{len_}]) T(::sus::move(t));
345346
len_ += 1_usize;
346347
}
347348

@@ -366,7 +367,7 @@ class Vec {
366367
{
367368
check(!is_moved_from());
368369
reserve(1_usize);
369-
new (as_mut_ptr() + size_t{len_}) T(::sus::forward<Us>(args)...);
370+
new (&storage_[size_t{len_}]) T(::sus::forward<Us>(args)...);
370371
len_ += 1_usize;
371372
}
372373

@@ -399,7 +400,7 @@ class Vec {
399400
/// will be invalid and Undefined Behaviour results.
400401
constexpr inline const T& get_unchecked(::sus::marker::UnsafeFnMarker,
401402
usize i) const& noexcept {
402-
return reinterpret_cast<T*>(storage_)[i.primitive_value];
403+
return storage_[i.primitive_value];
403404
}
404405
constexpr inline const T& get_unchecked(::sus::marker::UnsafeFnMarker,
405406
usize i) && = delete;
@@ -411,7 +412,7 @@ class Vec {
411412
/// Behaviour results.
412413
constexpr inline T& get_unchecked_mut(::sus::marker::UnsafeFnMarker,
413414
usize i) & noexcept {
414-
return reinterpret_cast<T*>(storage_)[i.primitive_value];
415+
return storage_[i.primitive_value];
415416
}
416417

417418
/// Present a nicer error when trying to use operator[] with an `int`,
@@ -461,7 +462,7 @@ class Vec {
461462
inline const T* as_ptr() const& noexcept {
462463
check(!is_moved_from());
463464
check(is_alloced());
464-
return reinterpret_cast<T*>(storage_);
465+
return storage_;
465466
}
466467
const T* as_ptr() && = delete;
467468

@@ -472,7 +473,7 @@ class Vec {
472473
inline T* as_mut_ptr() & noexcept {
473474
check(!is_moved_from());
474475
check(is_alloced());
475-
return reinterpret_cast<T*>(storage_);
476+
return storage_;
476477
}
477478

478479
// Returns a slice that references all the elements of the vector as const
@@ -481,8 +482,8 @@ class Vec {
481482
check(!is_moved_from());
482483
// SAFETY: The `len_` is the number of elements in the Vec, and the pointer
483484
// is to the start of the Vec, so this Slice covers a valid range.
484-
return Slice<const T>::from_raw_parts(
485-
::sus::marker::unsafe_fn, reinterpret_cast<const T*>(storage_), len_);
485+
return Slice<const T>::from_raw_parts(::sus::marker::unsafe_fn, storage_,
486+
len_);
486487
}
487488
constexpr Slice<const T> as_ref() && = delete;
488489

@@ -492,17 +493,15 @@ class Vec {
492493
check(!is_moved_from());
493494
// SAFETY: The `len_` is the number of elements in the Vec, and the pointer
494495
// is to the start of the Vec, so this Slice covers a valid range.
495-
return Slice<T>::from_raw_parts(::sus::marker::unsafe_fn,
496-
reinterpret_cast<T*>(storage_), len_);
496+
return Slice<T>::from_raw_parts(::sus::marker::unsafe_fn, storage_, len_);
497497
}
498498

499499
/// Returns an iterator over all the elements in the array, visited in the
500500
/// same order they appear in the array. The iterator gives const access to
501501
/// each element.
502502
constexpr SliceIter<const T&> iter() const& noexcept {
503503
check(!is_moved_from());
504-
return SliceIter<const T&>::with(reinterpret_cast<const T*>(storage_),
505-
len_);
504+
return SliceIter<const T&>::with(storage_, len_);
506505
}
507506
constexpr SliceIter<const T&> iter() && = delete;
508507

@@ -511,7 +510,7 @@ class Vec {
511510
/// each element.
512511
constexpr SliceIterMut<T&> iter_mut() & noexcept {
513512
check(!is_moved_from());
514-
return SliceIterMut<T&>::with(reinterpret_cast<T*>(storage_), len_);
513+
return SliceIterMut<T&>::with(storage_, len_);
515514
}
516515

517516
/// Converts the array into an iterator that consumes the array and returns
@@ -540,8 +539,7 @@ class Vec {
540539
inline void destroy_storage_objects() {
541540
if constexpr (!std::is_trivially_destructible_v<T>) {
542541
const size_t len = size_t{len_};
543-
for (auto i = size_t{0}; i < len; ++i)
544-
reinterpret_cast<T*>(storage_)[i].~T();
542+
for (auto i = size_t{0}; i < len; ++i) storage_[i].~T();
545543
}
546544
}
547545

0 commit comments

Comments
 (0)