Skip to content

Commit

Permalink
Merge pull request #24 from LukasKalbertodt/unit-tests
Browse files Browse the repository at this point in the history
Add many unit tests and fix two memory safety bugs
  • Loading branch information
LukasKalbertodt authored Jan 24, 2019
2 parents c3ca088 + 5863d4d commit 548aaba
Show file tree
Hide file tree
Showing 4 changed files with 553 additions and 58 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.3.1] - 2019-01-24
### Fixed
- Fix memory safety bug in `clone()`: cloning a non-compact stable vec before
this change accessed already dropped values and attempted to clone them.
- Fix memory safety bug in `clear()`: calling `clear()` on a non-compact
stable vec before would access already dropped values and drop them a second
time.

## [0.3.0] - 2019-01-07
### Removed
- Remove `IterMut::remove_current`. The method was broken because it did not
Expand Down Expand Up @@ -78,6 +86,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.


[Unreleased]: https://github.com/LukasKalbertodt/stable-vec/compare/v0.3.0...HEAD
[0.3.1]: https://github.com/LukasKalbertodt/stable-vec/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/LukasKalbertodt/stable-vec/compare/v0.2.2...v0.3.0
[0.2.2]: https://github.com/LukasKalbertodt/stable-vec/compare/v0.2.1...v0.2.2
[0.2.1]: https://github.com/LukasKalbertodt/stable-vec/compare/v0.2.0...v0.2.1
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stable-vec"
version = "0.3.0"
version = "0.3.1"
authors = ["Lukas Kalbertodt <[email protected]>"]

description = """
Expand Down
31 changes: 29 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod tests;
/// - [`shrink_to_fit()`](#method.shrink_to_fit)
/// - [`reserve()`](#method.reserve)
///
#[derive(Clone, PartialEq, Eq)]
#[derive(PartialEq, Eq)]
pub struct StableVec<T> {
/// Storing the actual data.
data: Vec<T>,
Expand Down Expand Up @@ -675,7 +675,15 @@ impl<T> StableVec<T> {
/// assert!(sv.capacity() >= 2);
/// ```
pub fn clear(&mut self) {
self.data.clear();
unsafe {
if mem::needs_drop::<T>() {
for e in &mut *self {
ptr::drop_in_place(e);
}
}
self.data.set_len(0);
}

self.deleted.truncate(0);
self.used_count = 0;
}
Expand Down Expand Up @@ -901,6 +909,25 @@ impl<T> StableVec<T> {
}
}

impl<T: Clone> Clone for StableVec<T> {
fn clone(&self) -> Self {
let mut new_vec = Vec::with_capacity(self.data.len());

unsafe {
new_vec.set_len(self.data.len());
for i in self.keys() {
ptr::write(new_vec.get_unchecked_mut(i), self[i].clone());
}
}

Self {
data: new_vec,
deleted: self.deleted.clone(),
used_count: self.used_count,
}
}
}

impl<T> Drop for StableVec<T> {
fn drop(&mut self) {
// We need to drop all elements that have not been removed. We can't
Expand Down
Loading

0 comments on commit 548aaba

Please sign in to comment.