Skip to content

Commit 612e99e

Browse files
Merge pull request Michael-F-Bryan#3 from elichai/2019-11-miri
Fix truncate 2 mutable references
2 parents 147553c + 99dc59a commit 612e99e

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ script:
99
- cargo build --verbose
1010
- cargo test --verbose
1111
- cargo doc --verbose
12+
- ./ci/miri.sh
1213

1314
before_deploy:
1415
- cargo doc --verbose

ci/miri.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env sh
2+
3+
set -ex
4+
5+
export CARGO_NET_RETRY=5
6+
export CARGO_NET_TIMEOUT=10
7+
8+
MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri)
9+
echo "Installing latest nightly with Miri: $MIRI_NIGHTLY"
10+
rustup default "$MIRI_NIGHTLY"
11+
12+
rustup component add miri
13+
cargo miri setup
14+
15+
cargo miri test

src/lib.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,14 @@ impl<T, const N: usize> ArrayVec<T, { N }> {
180180
pub fn truncate(&mut self, new_length: usize) {
181181
unsafe {
182182
if new_length < self.len() {
183-
let start = self.as_mut_ptr().add(new_length);
184183
let num_elements_to_remove = self.len() - new_length;
184+
// Start by setting the new length, so we can "pre-poop our pants" (http://cglab.ca/~abeinges/blah/everyone-poops/)
185+
self.set_len(new_length);
186+
187+
let start = self.as_mut_ptr().add(new_length);
185188
let tail: *mut [T] =
186189
slice::from_raw_parts_mut(start, num_elements_to_remove);
187190

188-
self.set_len(new_length);
189191
ptr::drop_in_place(tail);
190192
}
191193
}
@@ -475,3 +477,22 @@ impl<T> Display for CapacityError<T> {
475477
write!(f, "Insufficient capacity")
476478
}
477479
}
480+
481+
#[cfg(test)]
482+
mod tests {
483+
use super::ArrayVec;
484+
485+
#[test]
486+
fn test_equal_to_expected_slice() {
487+
let mut vector: ArrayVec<u8, 10> = ArrayVec::new();
488+
vector.push(0);
489+
vector.push(1);
490+
vector.push(2);
491+
assert_eq!(vector.len(), 3);
492+
493+
vector.try_insert(3, 3).unwrap();
494+
495+
assert_eq!(vector.as_slice(), &[0, 1, 2, 3]);
496+
assert_eq!(vector.capacity(), 10);
497+
}
498+
}

0 commit comments

Comments
 (0)