Skip to content

Commit

Permalink
Use a wrapper type for Vector so we can hide more internal types.
Browse files Browse the repository at this point in the history
Plus moar lints.
  • Loading branch information
bodil committed Dec 17, 2019
1 parent 1a3aef5 commit 22f6907
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 147 deletions.
44 changes: 22 additions & 22 deletions src/fakepool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ use std::sync::Arc as RArc;

use crate::nodes::chunk::Chunk;

pub trait PoolDefault: Default {}
pub trait PoolClone: Clone {}
pub(crate) trait PoolDefault: Default {}
pub(crate) trait PoolClone: Clone {}

impl<A> PoolDefault for Chunk<A> {}
impl<A> PoolClone for Chunk<A> where A: Clone {}

pub struct Pool<A>(PhantomData<A>);
pub(crate) struct Pool<A>(PhantomData<A>);

impl<A> Pool<A> {
pub fn new(_size: usize) -> Self {
pub(crate) fn new(_size: usize) -> Self {
Pool(PhantomData)
}

pub fn get_pool_size(&self) -> usize {
pub(crate) fn get_pool_size(&self) -> usize {
0
}

pub fn fill(&self) {}
pub(crate) fn fill(&self) {}
}

impl<A> Clone for Pool<A> {
Expand All @@ -40,44 +40,44 @@ impl<A> Clone for Pool<A> {
// Rc

#[derive(Default)]
pub struct Rc<A>(RRc<A>);
pub(crate) struct Rc<A>(RRc<A>);

impl<A> Rc<A> {
#[inline(always)]
pub fn default(_pool: &Pool<A>) -> Self
pub(crate) fn default(_pool: &Pool<A>) -> Self
where
A: PoolDefault,
{
Self(Default::default())
}

#[inline(always)]
pub fn new(_pool: &Pool<A>, value: A) -> Self {
pub(crate) fn new(_pool: &Pool<A>, value: A) -> Self {
Rc(RRc::new(value))
}

#[inline(always)]
pub fn clone_from(_pool: &Pool<A>, value: &A) -> Self
pub(crate) fn clone_from(_pool: &Pool<A>, value: &A) -> Self
where
A: PoolClone,
{
Rc(RRc::new(value.clone()))
}

#[inline(always)]
pub fn make_mut<'a>(_pool: &Pool<A>, this: &'a mut Self) -> &'a mut A
pub(crate) fn make_mut<'a>(_pool: &Pool<A>, this: &'a mut Self) -> &'a mut A
where
A: PoolClone,
{
RRc::make_mut(&mut this.0)
}

#[inline(always)]
pub fn ptr_eq(left: &Self, right: &Self) -> bool {
pub(crate) fn ptr_eq(left: &Self, right: &Self) -> bool {
RRc::ptr_eq(&left.0, &right.0)
}

pub fn unwrap_or_clone(this: Self) -> A
pub(crate) fn unwrap_or_clone(this: Self) -> A
where
A: PoolClone,
{
Expand Down Expand Up @@ -117,52 +117,52 @@ where
A: std::fmt::Debug,
{
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.0.fmt(f)
}
}

// Arc

#[derive(Default)]
pub struct Arc<A>(RArc<A>);
pub(crate) struct Arc<A>(RArc<A>);

impl<A> Arc<A> {
#[inline(always)]
pub fn default(_pool: &Pool<A>) -> Self
pub(crate) fn default(_pool: &Pool<A>) -> Self
where
A: PoolDefault,
{
Self(Default::default())
}

#[inline(always)]
pub fn new(_pool: &Pool<A>, value: A) -> Self {
pub(crate) fn new(_pool: &Pool<A>, value: A) -> Self {
Self(RArc::new(value))
}

#[inline(always)]
pub fn clone_from(_pool: &Pool<A>, value: &A) -> Self
pub(crate) fn clone_from(_pool: &Pool<A>, value: &A) -> Self
where
A: PoolClone,
{
Self(RArc::new(value.clone()))
}

#[inline(always)]
pub fn make_mut<'a>(_pool: &Pool<A>, this: &'a mut Self) -> &'a mut A
pub(crate) fn make_mut<'a>(_pool: &Pool<A>, this: &'a mut Self) -> &'a mut A
where
A: PoolClone,
{
RArc::make_mut(&mut this.0)
}

#[inline(always)]
pub fn ptr_eq(left: &Self, right: &Self) -> bool {
pub(crate) fn ptr_eq(left: &Self, right: &Self) -> bool {
RArc::ptr_eq(&left.0, &right.0)
}

pub fn unwrap_or_clone(this: Self) -> A
pub(crate) fn unwrap_or_clone(this: Self) -> A
where
A: PoolClone,
{
Expand Down Expand Up @@ -202,7 +202,7 @@ where
A: std::fmt::Debug,
{
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
self.0.fmt(f)
}
}
50 changes: 24 additions & 26 deletions src/nodes/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait BTreeValue {
fn cmp_values(&self, other: &Self) -> Ordering;
}

pub struct Node<A> {
pub(crate) struct Node<A> {
keys: Chunk<A, NodeSize>,
children: Chunk<Option<PoolRef<Node<A>>>, Add1<NodeSize>>,
}
Expand Down Expand Up @@ -73,10 +73,9 @@ where
}
}

pub enum Insert<A> {
pub(crate) enum Insert<A> {
Added,
Replaced(A),
Update(Node<A>),
Split(Node<A>, A, Node<A>),
}

Expand All @@ -87,7 +86,7 @@ enum InsertAction<A> {
InsertSplit(Node<A>, A, Node<A>),
}

pub enum Remove<A> {
pub(crate) enum Remove<A> {
NoChange,
Removed(A),
Update(A, Node<A>),
Expand Down Expand Up @@ -141,20 +140,20 @@ impl<A> Node<A> {
}

#[inline]
pub fn new() -> Self {
Self::default()
}

#[inline]
pub fn unit(value: A) -> Self {
pub(crate) fn unit(value: A) -> Self {
Node {
keys: Chunk::unit(value),
children: Chunk::pair(None, None),
}
}

#[inline]
pub fn new_from_split(pool: &Pool<Node<A>>, left: Node<A>, median: A, right: Node<A>) -> Self {
pub(crate) fn new_from_split(
pool: &Pool<Node<A>>,
left: Node<A>,
median: A,
right: Node<A>,
) -> Self {
Node {
keys: Chunk::unit(median),
children: Chunk::pair(
Expand All @@ -164,14 +163,14 @@ impl<A> Node<A> {
}
}

pub fn min(&self) -> Option<&A> {
pub(crate) fn min(&self) -> Option<&A> {
match self.children.first().unwrap() {
None => self.keys.first(),
Some(ref child) => child.min(),
}
}

pub fn max(&self) -> Option<&A> {
pub(crate) fn max(&self) -> Option<&A> {
match self.children.last().unwrap() {
None => self.keys.last(),
Some(ref child) => child.max(),
Expand All @@ -192,7 +191,7 @@ impl<A: BTreeValue> Node<A> {
}
}

pub fn lookup<BK>(&self, key: &BK) -> Option<&A>
pub(crate) fn lookup<BK>(&self, key: &BK) -> Option<&A>
where
BK: Ord + ?Sized,
A::Key: Borrow<BK>,
Expand All @@ -212,7 +211,7 @@ impl<A: BTreeValue> Node<A> {
}
}

pub fn lookup_mut<BK>(&mut self, pool: &Pool<Node<A>>, key: &BK) -> Option<&mut A>
pub(crate) fn lookup_mut<BK>(&mut self, pool: &Pool<Node<A>>, key: &BK) -> Option<&mut A>
where
A: Clone,
BK: Ord + ?Sized,
Expand All @@ -236,7 +235,7 @@ impl<A: BTreeValue> Node<A> {
}
}

pub fn path_first<'a, BK>(
pub(crate) fn path_first<'a, BK>(
&'a self,
mut path: Vec<(&'a Node<A>, usize)>,
) -> Vec<(&'a Node<A>, usize)>
Expand All @@ -260,7 +259,7 @@ impl<A: BTreeValue> Node<A> {
}
}

pub fn path_last<'a, BK>(
pub(crate) fn path_last<'a, BK>(
&'a self,
mut path: Vec<(&'a Node<A>, usize)>,
) -> Vec<(&'a Node<A>, usize)>
Expand All @@ -285,7 +284,7 @@ impl<A: BTreeValue> Node<A> {
}
}

pub fn path_next<'a, BK>(
pub(crate) fn path_next<'a, BK>(
&'a self,
key: &BK,
mut path: Vec<(&'a Node<A>, usize)>,
Expand Down Expand Up @@ -319,7 +318,7 @@ impl<A: BTreeValue> Node<A> {
}
}

pub fn path_prev<'a, BK>(
pub(crate) fn path_prev<'a, BK>(
&'a self,
key: &BK,
mut path: Vec<(&'a Node<A>, usize)>,
Expand Down Expand Up @@ -464,7 +463,7 @@ impl<A: BTreeValue> Node<A> {
self.children.push_back(child);
}

pub fn insert(&mut self, pool: &Pool<Node<A>>, value: A) -> Insert<A>
pub(crate) fn insert(&mut self, pool: &Pool<Node<A>>, value: A) -> Insert<A>
where
A: Clone,
{
Expand All @@ -490,7 +489,6 @@ impl<A: BTreeValue> Node<A> {
match child.insert(pool, value.clone()) {
Insert::Added => AddedAction,
Insert::Replaced(value) => ReplacedAction(value),
Insert::Update(_) => unreachable!(),
Insert::Split(left, median, right) => InsertSplit(left, median, right),
}
}
Expand Down Expand Up @@ -526,7 +524,7 @@ impl<A: BTreeValue> Node<A> {
self.split(pool, median, left, right)
}

pub fn remove<BK>(&mut self, pool: &Pool<Node<A>>, key: &BK) -> Remove<A>
pub(crate) fn remove<BK>(&mut self, pool: &Pool<Node<A>>, key: &BK) -> Remove<A>
where
A: Clone,
BK: Ord + ?Sized,
Expand Down Expand Up @@ -823,8 +821,8 @@ pub struct Iter<'a, A> {
pub(crate) remaining: usize,
}

impl<'a, A: 'a + BTreeValue> Iter<'a, A> {
pub fn new<R, BK>(root: &'a Node<A>, size: usize, range: R) -> Self
impl<'a, A: BTreeValue> Iter<'a, A> {
pub(crate) fn new<R, BK>(root: &'a Node<A>, size: usize, range: R) -> Self
where
R: RangeBounds<BK>,
A::Key: Borrow<BK>,
Expand Down Expand Up @@ -1013,7 +1011,7 @@ pub struct ConsumingIter<A> {
}

impl<A: Clone> ConsumingIter<A> {
pub fn new(root: &Node<A>, total: usize) -> Self {
pub(crate) fn new(root: &Node<A>, total: usize) -> Self {
ConsumingIter {
fwd_last: None,
fwd_stack: vec![ConsumingIterItem::Consider(root.clone())],
Expand Down Expand Up @@ -1146,7 +1144,7 @@ enum IterItem<'a, A> {
}

impl<'a, A: 'a> DiffIter<'a, A> {
pub fn new(old: &'a Node<A>, new: &'a Node<A>) -> Self {
pub(crate) fn new(old: &'a Node<A>, new: &'a Node<A>) -> Self {
DiffIter {
old_stack: if old.keys.is_empty() {
Vec::new()
Expand Down

0 comments on commit 22f6907

Please sign in to comment.