Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test drop with Rc #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 51 additions & 54 deletions src/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,113 +10,110 @@ impl <T, const N: usize> Drop for LocalVec<T, N> {

#[cfg(test)]
mod tests {
struct CounterGuard(*mut u8);
use crate::LocalVec;
use std::rc::Rc;

#[derive(Clone)]
struct Counter(Rc<()>);

impl<'a> CounterGuard {
pub fn new(cnt: &'a mut u8) -> CounterGuard {
*cnt += 1;
CounterGuard(cnt as *mut u8)
impl Counter {
fn new() -> Self {
Self(Rc::new(()))
}
}

impl<'a> Drop for CounterGuard {
fn drop(&mut self) {
unsafe {
*self.0 -= 1;
}
fn count(&self) -> usize {
Rc::strong_count(&self.0)
}
}

use crate::LocalVec;

#[test]
fn test_drop() {
let mut cnt = 0u8;
let cnt = Counter::new();
let mut buf = LocalVec::<_, 3>::new();

assert_eq!(cnt, 0);
assert_eq!(cnt.count(), 1);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 1);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 2);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 2);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 3);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 3);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 4);

std::mem::drop(buf);
assert_eq!(cnt, 0);
assert_eq!(cnt.count(), 1);
}

#[test]
fn test_drop_after_set_len() {
let mut cnt = 0u8;
let cnt = Counter::new();
let mut buf = LocalVec::<_, 3>::new();

assert_eq!(cnt, 0);
assert_eq!(cnt.count(), 1);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 1);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 2);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 2);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 3);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 3);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 4);

unsafe {
buf.set_len(1);
}

std::mem::drop(buf);
assert_eq!(cnt, 2);
assert_eq!(cnt.count(), 3);
}

#[test]
fn test_drop_after_into_array() {
let mut cnt = 0u8;
let cnt = Counter::new();
let mut buf = LocalVec::<_, 3>::new();

assert_eq!(cnt, 0);
assert_eq!(cnt.count(), 1);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 1);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 2);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 2);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 3);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 3);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 4);

let arr: [_; 3] = buf.into();
assert_eq!(cnt, 3);
let arr: [Counter; 3] = buf.try_into().unwrap();
assert_eq!(cnt.count(), 4);

std::mem::drop(arr);
assert_eq!(cnt, 0);
assert_eq!(cnt.count(), 1);
}

#[test]
fn test_drop_after_take_array() {
let mut cnt = 0u8;
let cnt = Counter::new();
let mut buf = LocalVec::<_, 3>::new();

assert_eq!(cnt, 0);
assert_eq!(cnt.count(), 1);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 1);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 2);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 2);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 3);

buf.push(CounterGuard::new(&mut cnt));
assert_eq!(cnt, 3);
buf.push(cnt.clone());
assert_eq!(cnt.count(), 4);

let arr = buf.take_array();
let arr = unsafe { buf.take_array() };
assert_eq!(buf.len(), 0);
assert_eq!(cnt, 3);
assert_eq!(cnt.count(), 4);

std::mem::drop(arr);
assert_eq!(cnt, 0);
assert_eq!(cnt.count(), 1);
}
}
}
36 changes: 30 additions & 6 deletions src/from.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
use std::convert::From;
use crate::LocalVec;
use std::convert::{From, TryFrom};
use std::mem::MaybeUninit;

impl<T, const N: usize> From<LocalVec<T, N>> for [T; N] {
impl<T, const N: usize> From<LocalVec<T, N>> for [MaybeUninit<T>; N] {
fn from(mut local_vec: LocalVec<T, N>) -> Self {
local_vec.take_array()
local_vec.take_uninit_array()
}
}

#[derive(Debug)]
pub struct NotFull;

impl std::fmt::Display for NotFull {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("array is not full")
}
}

impl std::error::Error for NotFull {}

impl<T, const N: usize> TryFrom<LocalVec<T, N>> for [T; N] {
type Error = NotFull;

fn try_from(mut local_vec: LocalVec<T, N>) -> Result<Self, Self::Error> {
if !local_vec.is_full() {
return Err(NotFull);
}
// Safety: checked for is_full before.
Ok(unsafe { local_vec.take_array() })
}
}

Expand All @@ -13,16 +37,16 @@ mod test {

#[test]
fn test_into_array() {
let mut vec = LocalVec::<_, 4>::new();
let mut vec = LocalVec::<u32, 4>::new();
vec.push(0);
vec.push(1);
vec.push(2);
vec.push(3);

let arr: [_; 4] = vec.into();
let arr: [u32; 4] = vec.try_into().unwrap();
assert_eq!(arr[0], 0);
assert_eq!(arr[1], 1);
assert_eq!(arr[2], 2);
assert_eq!(arr[3], 3);
}
}
}
34 changes: 27 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,23 @@ impl<T, const N: usize> LocalVecImpl<T, N> {
}

#[must_use = "consider using clear() instead"]
/// steal the elements stored
pub fn take_array(&mut self) -> [T; N] {
let arr: [T; N] = unsafe {
/// Takes possibly uninitialized elements.
pub fn take_uninit_array(&mut self) -> [MaybeUninit<T>; N] {
unsafe {
self.set_len(0);
std::mem::transmute_copy(&self.buf)
};
arr
std::ptr::read(&self.buf)
}
}

#[must_use = "consider using clear() instead"]
/// Takes the stored elements.
///
/// # Safety
/// The container must be full.
pub unsafe fn take_array(&mut self) -> [T; N] {
debug_assert!(self.is_full());
let prev = std::mem::replace(self, LocalVecImpl::new());
std::mem::transmute_copy(&prev.buf)
}

#[inline]
Expand Down Expand Up @@ -312,10 +322,20 @@ mod tests {

#[test]
fn test_take_array() {
let arr = [2; 4];
let mut vec = LocalVecImpl::<_, 4>::from_array(arr);
assert_eq!(vec.len(), 4);
let taken = unsafe { vec.take_array() };
assert_eq!(arr, taken);
assert_eq!(vec.len(), 0);
}

#[test]
fn test_take_uninit_array() {
let arr = [7; 4];
let mut vec = LocalVecImpl::<_, 6>::from_array(arr);
assert_eq!(vec.len(), 4);
let _ = vec.take_array();
let _ = vec.take_uninit_array();
assert_eq!(vec.len(), 0);
}

Expand Down