Skip to content

Commit 4749fc2

Browse files
author
bors-servo
authored
Auto merge of #88 - Vurich:array-zero, r=jdm
Add Array impl for [T; 0] This seems useless but has benefits in generic contexts (plus, with smart-enough compiler optimisations this could conceivably compile to equivalent code to `Vec<T>`). <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/88) <!-- Reviewable:end -->
2 parents 0be8bb0 + cccd873 commit 4749fc2

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,14 +1132,14 @@ macro_rules! impl_array(
11321132
unsafe impl<T> Array for [T; $size] {
11331133
type Item = T;
11341134
fn size() -> usize { $size }
1135-
fn ptr(&self) -> *const T { &self[0] }
1136-
fn ptr_mut(&mut self) -> *mut T { &mut self[0] }
1135+
fn ptr(&self) -> *const T { self.as_ptr() }
1136+
fn ptr_mut(&mut self) -> *mut T { self.as_mut_ptr() }
11371137
}
11381138
)+
11391139
}
11401140
);
11411141

1142-
impl_array!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36,
1142+
impl_array!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36,
11431143
0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000,
11441144
0x10000, 0x20000, 0x40000, 0x80000, 0x100000);
11451145

@@ -1162,6 +1162,15 @@ mod tests {
11621162
#[cfg(not(feature = "std"))]
11631163
use alloc::vec::Vec;
11641164

1165+
#[test]
1166+
pub fn test_zero() {
1167+
let mut v = SmallVec::<[_; 0]>::new();
1168+
assert!(!v.spilled());
1169+
v.push(0usize);
1170+
assert!(v.spilled());
1171+
assert_eq!(&*v, &[0]);
1172+
}
1173+
11651174
// We heap allocate all these strings so that double frees will show up under valgrind.
11661175

11671176
#[test]

0 commit comments

Comments
 (0)