Skip to content

Commit f0307c3

Browse files
Some additional refactoring and test adjustments.
Hopefully the weirdness with running Miri specifically on Mac is gone.
1 parent 2b5337b commit f0307c3

File tree

3 files changed

+102
-27
lines changed

3 files changed

+102
-27
lines changed

src/iterators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<'a, T: 'a, const N: usize> Iterator for StaticVecDrain<'a, T, N> {
713713
type Item = T;
714714

715715
#[inline(always)]
716-
fn next(&mut self) -> Option<T> {
716+
fn next(&mut self) -> Option<T> {
717717
self
718718
.iter
719719
.next()

src/utils.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use core::mem::{size_of, MaybeUninit};
55
use crate::StaticVec;
66

77
#[inline(always)]
8-
pub(crate) fn runtime_zst_handler<T>(dest: *const T, origin: *const T) -> usize {
8+
pub(crate) fn runtime_distance_between<T>(dest: *const T, origin: *const T) -> usize {
99
// At runtime we can handle ZSTs with usize casts, as is done throughout the crate.
1010
(dest as usize).wrapping_sub(origin as usize)
1111
}
1212

1313
#[inline(always)]
14-
pub(crate) const fn compiletime_zst_handler<T>(_dest: *const T, _origin: *const T) -> usize {
14+
pub(crate) const fn compiletime_distance_between<T>(_dest: *const T, _origin: *const T) -> usize {
1515
assert!(
1616
size_of::<T>() == 0,
1717
"`compiletime_zst_handler` called on a non-ZST!"
@@ -28,7 +28,13 @@ pub(crate) const fn distance_between<T>(dest: *const T, origin: *const T) -> usi
2828
match size_of::<T>() {
2929
// This function cannot safely be used on ZST pointers at compile time (which would not be
3030
// useful in any scenario I can currently think of anyways).
31-
0 => unsafe { const_eval_select((dest, origin), compiletime_zst_handler, runtime_zst_handler) },
31+
0 => unsafe {
32+
const_eval_select(
33+
(dest, origin),
34+
compiletime_distance_between,
35+
runtime_distance_between,
36+
)
37+
},
3238
// For all other sizes of `T` though, `offset_from` works just fine.
3339
_ => unsafe { dest.offset_from(origin) as usize },
3440
}
@@ -102,11 +108,31 @@ where T: Copy {
102108
}
103109
}
104110

111+
#[inline(always)]
112+
pub(crate) const fn compiletime_zst_ptr_add<T>(ptr: *const T, count: usize) -> *const T {
113+
(ptr as *const u8).wrapping_add(count) as *const T
114+
}
115+
116+
#[inline(always)]
117+
pub(crate) const fn compiletime_zst_ptr_add_mut<T>(ptr: *mut T, count: usize) -> *mut T {
118+
(ptr as *mut u8).wrapping_add(count) as *mut T
119+
}
120+
121+
#[inline(always)]
122+
pub(crate) fn runtime_zst_ptr_add<T>(ptr: *const T, count: usize) -> *const T {
123+
(ptr as usize + count) as *const T
124+
}
125+
126+
#[inline(always)]
127+
pub(crate) fn runtime_zst_ptr_add_mut<T>(ptr: *mut T, count: usize) -> *mut T {
128+
(ptr as usize + count) as *mut T
129+
}
130+
105131
/// An internal convenience function for incrementing immutable ZST pointers by usize offsets.
106132
#[inline(always)]
107133
pub(crate) const fn zst_ptr_add<T>(ptr: *const T, count: usize) -> *const T {
108134
debug_assert!(size_of::<T>() == 0, "`zst_ptr_add` called on a non-ZST!");
109-
(ptr as *const u8).wrapping_add(count) as *const T
135+
unsafe { const_eval_select((ptr, count), compiletime_zst_ptr_add, runtime_zst_ptr_add) }
110136
}
111137

112138
/// An internal convenience function for incrementing mutable ZST pointers by usize offsets.
@@ -116,7 +142,13 @@ pub(crate) const fn zst_ptr_add_mut<T>(ptr: *mut T, count: usize) -> *mut T {
116142
size_of::<T>() == 0,
117143
"`zst_ptr_add_mut` called on a non-ZST!"
118144
);
119-
(ptr as *mut u8).wrapping_add(count) as *mut T
145+
unsafe {
146+
const_eval_select(
147+
(ptr, count),
148+
compiletime_zst_ptr_add_mut,
149+
runtime_zst_ptr_add_mut,
150+
)
151+
}
120152
}
121153

122154
/// A version of the default `partial_cmp` implementation with a more flexible function signature.

test/test_staticvec.rs

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -498,31 +498,14 @@ fn drain_iter() {
498498
ZST {},
499499
ZST {},
500500
];
501-
let mut v5_drain = v5.drain_iter(6..12);
502-
assert_eq!(v5_drain.len(), 6);
503-
while let Some(_x) = v5_drain.next() {}
504-
drop(v5_drain);
501+
assert_eq!(v5.drain_iter(6..12).len(), 6);
505502
assert_eq!(v5.len(), 10);
506503
let mut v6 = staticvec![
507-
box 1,
508-
box 2,
509-
box 3,
510-
box 4,
511-
box 5,
512-
box 6,
513-
box 7,
514-
box 8,
515-
box 9,
516-
box 10,
517-
box 11,
518-
box 12,
519-
box 13,
520-
box 14,
521-
box 15,
522-
box 16,
504+
box 1, box 2, box 3, box 4, box 5, box 6, box 7, box 8, box 9, box 10, box 11, box 12, box 13,
505+
box 14, box 15, box 16,
523506
];
524507
assert_eq!(v6.drain_iter(6..12).len(), 6);
525-
assert_eq!(v6.len(), 10);
508+
assert_eq!(v6.len(), 10);
526509
assert_eq!(
527510
staticvec![1, 2, 3, 4]
528511
.drain_iter(1..3)
@@ -1150,6 +1133,26 @@ fn iter_nth() {
11501133
let o = it7.nth(5);
11511134
assert_eq!(format!("{:?}", o), "Some([6, 6])");
11521135
assert_eq!(format!("{:?}", it7), "StaticVecIterConst([])");
1136+
let vlast = staticvec![
1137+
ZST {},
1138+
ZST {},
1139+
ZST {},
1140+
ZST {},
1141+
ZST {},
1142+
ZST {},
1143+
ZST {},
1144+
ZST {},
1145+
ZST {},
1146+
ZST {},
1147+
ZST {},
1148+
ZST {},
1149+
ZST {},
1150+
ZST {},
1151+
ZST {},
1152+
ZST {}
1153+
];
1154+
let mut itlast = vlast.iter();
1155+
assert_eq!(itlast.nth(13).unwrap(), &ZST {});
11531156
}
11541157

11551158
#[test]
@@ -1379,6 +1382,26 @@ fn iter_mut_nth() {
13791382
let o = it7.nth(5);
13801383
assert_eq!(format!("{:?}", o), "Some([6, 6])");
13811384
assert_eq!(format!("{:?}", it7), "StaticVecIterMut([])");
1385+
let mut vlast = staticvec![
1386+
ZST {},
1387+
ZST {},
1388+
ZST {},
1389+
ZST {},
1390+
ZST {},
1391+
ZST {},
1392+
ZST {},
1393+
ZST {},
1394+
ZST {},
1395+
ZST {},
1396+
ZST {},
1397+
ZST {},
1398+
ZST {},
1399+
ZST {},
1400+
ZST {},
1401+
ZST {}
1402+
];
1403+
let mut itlast = vlast.iter_mut();
1404+
assert_eq!(itlast.nth(13).unwrap(), &mut ZST {});
13821405
}
13831406

13841407
#[test]
@@ -1654,6 +1677,26 @@ fn into_iter_nth() {
16541677
let o = it7.nth(5);
16551678
assert_eq!(format!("{:?}", o), "Some([6, 6])");
16561679
assert_eq!(format!("{:?}", it7), "StaticVecIntoIter([])");
1680+
let vlast = staticvec![
1681+
ZST {},
1682+
ZST {},
1683+
ZST {},
1684+
ZST {},
1685+
ZST {},
1686+
ZST {},
1687+
ZST {},
1688+
ZST {},
1689+
ZST {},
1690+
ZST {},
1691+
ZST {},
1692+
ZST {},
1693+
ZST {},
1694+
ZST {},
1695+
ZST {},
1696+
ZST {}
1697+
];
1698+
let mut itlast = vlast.into_iter();
1699+
assert_eq!(itlast.nth(13).unwrap(), ZST {});
16571700
}
16581701

16591702
#[test]

0 commit comments

Comments
 (0)