Skip to content
Merged
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "im-lists"
version = "0.10.0"
version = "0.11.0"
authors = ["mattwparas <matthewparas2020@u.northwestern.edu>"]
edition = "2021"
readme = "README.md"
Expand All @@ -14,6 +14,7 @@ categories = ["data-structures"]

[dependencies]
smallvec = { version = "1.10.0" }
generic_singleton = "0.5.1"

[dev-dependencies]
criterion = "0.3.5"
Expand Down
92 changes: 84 additions & 8 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{cmp::Ordering, iter::FromIterator, marker::PhantomData};
use crate::{
handler::{DefaultDropHandler, DropHandler},
shared::{ArcPointer, PointerFamily, RcPointer},
unrolled::{ConsumingWrapper, IterWrapper, UnrolledList},
unrolled::{ConsumingWrapper, IterWrapper, UnrolledCell, UnrolledList},
};

/// A persistent list.
Expand Down Expand Up @@ -55,7 +55,7 @@ use crate::{
/// This means for operations that for a normal linked list may take linear time *Θ(n)*, we get a constant factor
/// decrease of either a factor of *m* or *m / 2*. Similarly, we will see O(log(n)) performance characteristics if the growth rate is set to be larger than 1.
pub struct GenericList<
T: Clone,
T: Clone + 'static,
P: PointerFamily = RcPointer,
const N: usize = 256,
const G: usize = 1,
Expand All @@ -68,6 +68,16 @@ pub type List<T> = GenericList<T, RcPointer, 256>;
pub type SharedVList<T> = GenericList<T, ArcPointer, 2, 2>;
pub type VList<T> = GenericList<T, RcPointer, 2, 2>;

#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct RawCell<
T: Clone + 'static,
P: PointerFamily,
const N: usize,
const G: usize,
D: DropHandler<GenericList<T, P, N, G, D>>,
>(*const UnrolledCell<T, P, N, G>, PhantomData<D>);

impl<T: Clone, P: PointerFamily, const N: usize, const G: usize, D: DropHandler<Self>> Clone
for GenericList<T, P, N, G, D>
{
Expand Down Expand Up @@ -140,8 +150,21 @@ impl<T: Clone, P: PointerFamily, const N: usize, const G: usize, D: DropHandler<
}

#[doc(hidden)]
pub fn draining_iterator(mut self) -> impl Iterator<Item = T> {
pub fn draining_iterator(
mut self,
// default: UnrolledList<T, P, N, G>,
) -> impl Iterator<Item = T> {
std::mem::take(&mut self.0).draining_iterator()
// std::mem::replace(&mut self.0, default).draining_iterator()
// todo!()
// let x = MaybeUninit::new(self);
// let x = x.as_ptr();

// unsafe { std::ptr::read(&(*x).0).draining_iterator() }

// self.0.draining_iterator()
// ManuallyDrop::take(slot)
// todo!()
}

#[doc(hidden)]
Expand All @@ -156,6 +179,32 @@ impl<T: Clone, P: PointerFamily, const N: usize, const G: usize, D: DropHandler<
.collect()
}

#[doc(hidden)]
pub fn as_ptr(&self) -> RawCell<T, P, N, G, D> {
RawCell(self.0.as_ptr(), PhantomData)
}

/// Call a function on a raw pointer
/// # Safety
/// This must be called with a valid pointer as returned from as_ptr
#[doc(hidden)]
pub unsafe fn call_from_raw<O, F: FnOnce(&Self) -> O>(
cell: RawCell<T, P, N, G, D>,
func: F,
) -> O {
let value = unsafe { Self::from_raw(cell) };
let res = func(&value);
std::mem::forget(value);
res
}

/// # Safety
/// This must be called with a valid pointer as returned from as_ptr
#[doc(hidden)]
unsafe fn from_raw(cell: RawCell<T, P, N, G, D>) -> Self {
Self(UnrolledList(P::from_raw(cell.0)), PhantomData)
}

/// Get the length of the list
///
/// # Examples
Expand Down Expand Up @@ -593,7 +642,7 @@ impl<
/// An iterator over lists with values of type `T`.
pub struct Iter<
'a,
T: Clone,
T: Clone + 'static,
P: PointerFamily,
const N: usize,
const G: usize,
Expand All @@ -602,7 +651,7 @@ pub struct Iter<

impl<
'a,
T: Clone,
T: Clone + 'static,
P: PointerFamily,
const N: usize,
const G: usize,
Expand Down Expand Up @@ -651,7 +700,7 @@ impl<

/// A consuming iterator over lists with values of type `T`.
pub struct ConsumingIter<
T: Clone,
T: Clone + 'static,
P: PointerFamily,
const N: usize,
const G: usize,
Expand Down Expand Up @@ -851,8 +900,15 @@ mod tests {
use crate::{list, vlist};

#[test]
fn strong_count() {
fn strong_count_empty() {
let list: List<usize> = List::new();
assert!(list.strong_count() >= 1);
}

#[test]
fn strong_count() {
let mut list: List<usize> = List::new();
list.cons_mut(1);
assert_eq!(list.strong_count(), 1);
}

Expand Down Expand Up @@ -1219,8 +1275,15 @@ mod arc_tests {
use crate::{shared_list, shared_vlist, vlist};

#[test]
fn strong_count() {
fn strong_count_empty() {
let list: SharedList<usize> = SharedList::new();
assert!(list.strong_count() >= 1);
}

#[test]
fn strong_count() {
let mut list: SharedList<usize> = SharedList::new();
list.cons_mut(1);
assert_eq!(list.strong_count(), 1);
}

Expand Down Expand Up @@ -1587,4 +1650,17 @@ mod arc_tests {

// assert!(list.is_empty())
}

#[test]
fn raw_test() {
let list = (0..1000usize).into_iter().collect::<SharedVList<_>>();

// Get the inner pointer, and then otherwise
// call the drop implementation as neatly as possible.
let pointer = list.as_ptr();

// Create value from pointer
let value = unsafe { SharedVList::from_raw(pointer) };
std::mem::forget(value);
}
}
60 changes: 40 additions & 20 deletions src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{ops::Deref, rc::Rc, sync::Arc};

pub trait PointerFamily {
type Pointer<T>: Deref<Target = T>;
pub trait PointerFamily: 'static {
type Pointer<T: 'static>: Deref<Target = T>;

fn new<T>(value: T) -> Self::Pointer<T>;
fn strong_count<T>(this: &Self::Pointer<T>) -> usize;
Expand All @@ -11,80 +11,100 @@ pub trait PointerFamily {
fn make_mut<T: Clone>(ptr: &mut Self::Pointer<T>) -> &mut T;
fn clone<T>(ptr: &Self::Pointer<T>) -> Self::Pointer<T>;
fn as_ptr<T>(this: &Self::Pointer<T>) -> *const T;
fn into_raw<T>(this: Self::Pointer<T>) -> *const T;
/// # Safety
/// This must be called from a pointer as returned from into_raw
unsafe fn from_raw<T>(this: *const T) -> Self::Pointer<T>;
}

pub struct RcPointer;

impl PointerFamily for RcPointer {
type Pointer<T> = Rc<T>;
type Pointer<T: 'static> = Rc<T>;

fn new<T>(value: T) -> Self::Pointer<T> {
fn new<T: 'static>(value: T) -> Self::Pointer<T> {
Rc::new(value)
}

fn strong_count<T>(this: &Self::Pointer<T>) -> usize {
fn strong_count<T: 'static>(this: &Self::Pointer<T>) -> usize {
Rc::strong_count(this)
}

fn try_unwrap<T>(this: Self::Pointer<T>) -> Option<T> {
fn try_unwrap<T: 'static>(this: Self::Pointer<T>) -> Option<T> {
Rc::try_unwrap(this).ok()
}

fn get_mut<T>(this: &mut Self::Pointer<T>) -> Option<&mut T> {
fn get_mut<T: 'static>(this: &mut Self::Pointer<T>) -> Option<&mut T> {
Rc::get_mut(this)
}

fn ptr_eq<T>(this: &Self::Pointer<T>, other: &Self::Pointer<T>) -> bool {
fn ptr_eq<T: 'static>(this: &Self::Pointer<T>, other: &Self::Pointer<T>) -> bool {
Rc::ptr_eq(this, other)
}

fn make_mut<T: Clone>(ptr: &mut Self::Pointer<T>) -> &mut T {
fn make_mut<T: Clone + 'static>(ptr: &mut Self::Pointer<T>) -> &mut T {
Rc::make_mut(ptr)
}

fn clone<T>(ptr: &Self::Pointer<T>) -> Self::Pointer<T> {
fn clone<T: 'static>(ptr: &Self::Pointer<T>) -> Self::Pointer<T> {
Rc::clone(ptr)
}

fn as_ptr<T>(this: &Self::Pointer<T>) -> *const T {
fn as_ptr<T: 'static>(this: &Self::Pointer<T>) -> *const T {
Rc::as_ptr(this)
}

fn into_raw<T: 'static>(this: Self::Pointer<T>) -> *const T {
Rc::into_raw(this)
}

unsafe fn from_raw<T: 'static>(this: *const T) -> Self::Pointer<T> {
Rc::from_raw(this)
}
}

pub struct ArcPointer;

impl PointerFamily for ArcPointer {
type Pointer<T> = Arc<T>;
type Pointer<T: 'static> = Arc<T>;

fn new<T>(value: T) -> Self::Pointer<T> {
fn new<T: 'static>(value: T) -> Self::Pointer<T> {
Arc::new(value)
}

fn strong_count<T>(this: &Self::Pointer<T>) -> usize {
fn strong_count<T: 'static>(this: &Self::Pointer<T>) -> usize {
Arc::strong_count(this)
}

fn try_unwrap<T>(this: Self::Pointer<T>) -> Option<T> {
fn try_unwrap<T: 'static>(this: Self::Pointer<T>) -> Option<T> {
Arc::try_unwrap(this).ok()
}

fn get_mut<T>(this: &mut Self::Pointer<T>) -> Option<&mut T> {
fn get_mut<T: 'static>(this: &mut Self::Pointer<T>) -> Option<&mut T> {
Arc::get_mut(this)
}

fn ptr_eq<T>(this: &Self::Pointer<T>, other: &Self::Pointer<T>) -> bool {
fn ptr_eq<T: 'static>(this: &Self::Pointer<T>, other: &Self::Pointer<T>) -> bool {
Arc::ptr_eq(this, other)
}

fn make_mut<T: Clone>(ptr: &mut Self::Pointer<T>) -> &mut T {
fn make_mut<T: Clone + 'static>(ptr: &mut Self::Pointer<T>) -> &mut T {
Arc::make_mut(ptr)
}

fn clone<T>(ptr: &Self::Pointer<T>) -> Self::Pointer<T> {
fn clone<T: 'static>(ptr: &Self::Pointer<T>) -> Self::Pointer<T> {
Arc::clone(ptr)
}

fn as_ptr<T>(this: &Self::Pointer<T>) -> *const T {
fn as_ptr<T: 'static>(this: &Self::Pointer<T>) -> *const T {
Arc::as_ptr(this)
}

fn into_raw<T: 'static>(this: Self::Pointer<T>) -> *const T {
Arc::into_raw(this)
}

unsafe fn from_raw<T: 'static>(this: *const T) -> Self::Pointer<T> {
Arc::from_raw(this)
}
}
Loading
Loading