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

Add Shared type to fix inference fail on nightly #2

Merged
merged 1 commit into from
Sep 27, 2017
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
33 changes: 17 additions & 16 deletions src/conslist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::ops::Deref;
use std::hash::{Hash, Hasher};
use std::cmp::Ordering;
use std::borrow::Borrow;
use shared::Shared;

use self::ConsListNode::{Cons, Nil};

Expand Down Expand Up @@ -113,7 +114,7 @@ macro_rules! conslist {
/// untrained ear.
pub fn cons<A, RA, RD>(car: RA, cdr: RD) -> ConsList<A>
where
Arc<A>: From<RA>,
RA: Shared<A>,
RD: Borrow<ConsList<A>>,
{
cdr.borrow().cons(car)
Expand Down Expand Up @@ -149,9 +150,9 @@ impl<A> ConsList<A> {
/// Construct a list with a single element.
pub fn singleton<R>(v: R) -> ConsList<A>
where
Arc<A>: From<R>,
R: Shared<A>,
{
ConsList(Arc::new(Cons(1, Arc::from(v), conslist![])))
ConsList(Arc::new(Cons(1, v.shared(), conslist![])))
}

/// Construct a list by consuming an `IntoIterator`.
Expand All @@ -176,9 +177,9 @@ impl<A> ConsList<A> {
pub fn from<R, I>(it: I) -> ConsList<A>
where
I: IntoIterator<Item = R>,
Arc<A>: From<R>,
R: Shared<A>,
{
it.into_iter().map(|a| Arc::from(a)).collect()
it.into_iter().map(|a| a.shared()).collect()
}

/// Test whether a list is empty.
Expand All @@ -197,9 +198,9 @@ impl<A> ConsList<A> {
/// Time: O(1)
pub fn cons<R>(&self, car: R) -> ConsList<A>
where
Arc<A>: From<R>,
R: Shared<A>,
{
ConsList(Arc::new(Cons(self.len() + 1, Arc::from(car), self.clone())))
ConsList(Arc::new(Cons(self.len() + 1, car.shared(), self.clone())))
}

/// Get the first element of a list.
Expand Down Expand Up @@ -491,9 +492,9 @@ where
/// ```
pub fn insert<T>(&self, item: T) -> ConsList<A>
where
Arc<A>: From<T>,
T: Shared<A>,
{
self.insert_ref(Arc::from(item))
self.insert_ref(item.shared())
}

fn insert_ref(&self, item: Arc<A>) -> ConsList<A> {
Expand Down Expand Up @@ -709,7 +710,7 @@ impl<A> Sum for ConsList<A> {

impl<A, T> FromIterator<T> for ConsList<A>
where
Arc<A>: From<T>,
T: Shared<A>,
{
fn from_iter<I>(source: I) -> Self
where
Expand All @@ -726,28 +727,28 @@ where

impl<'a, A, R> From<&'a [R]> for ConsList<A>
where
Arc<A>: From<&'a R>,
&'a R: Shared<A>,
{
fn from(slice: &'a [R]) -> Self {
slice.into_iter().map(|a| Arc::from(a)).collect()
slice.into_iter().map(|a| a.shared()).collect()
}
}

impl<A, R> From<Vec<R>> for ConsList<A>
where
Arc<A>: From<R>,
R: Shared<A>,
{
fn from(vec: Vec<R>) -> Self {
vec.into_iter().map(|a| Arc::from(a)).collect()
vec.into_iter().map(|a| a.shared()).collect()
}
}

impl<'a, A, R> From<&'a Vec<R>> for ConsList<A>
where
Arc<A>: From<&'a R>,
&'a R: Shared<A>,
{
fn from(vec: &'a Vec<R>) -> Self {
vec.into_iter().map(|a| Arc::from(a)).collect()
vec.into_iter().map(|a| a.shared()).collect()
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use std::marker::PhantomData;
use std::sync::Arc;

use shared::Shared;

/// A lens from `From` to `To` where the focus of the lens isn't guaranteed to
/// exist inside `From`. Operations on these lenses therefore return `Option`s.
pub trait PartialLens: Clone {
Expand All @@ -31,7 +33,7 @@ pub trait PartialLens: Clone {
/// operation succeeded.
fn try_put<Convert>(&self, v: Option<Convert>, s: &Self::From) -> Option<Self::From>
where
Arc<Self::To>: From<Convert>;
Convert: Shared<Self::To>;

/// Compose this lens with a lens from `To` to a new type `Next`, yielding
/// a lens from `From` to `Next`.
Expand All @@ -57,7 +59,7 @@ pub trait Lens: PartialLens {
/// Put a value into the lens, returning the updated `From` value.
fn put<Convert>(&self, v: Convert, s: &Self::From) -> Self::From
where
Arc<Self::To>: From<Convert>,
Convert: Shared<Self::To>,
{
self.try_put(Some(v), s).unwrap()
}
Expand Down Expand Up @@ -117,7 +119,7 @@ impl<A, B, C, L, R> PartialLens for Compose<A, B, C, L, R>

fn try_put<FromC>(&self, v: Option<FromC>, s: &A) -> Option<A>
where
Arc<C>: From<FromC>,
FromC: Shared<C>,
{
self.left
.try_get(&s)
Expand Down Expand Up @@ -184,9 +186,9 @@ impl<A, B> PartialLens for GeneralLens<A, B> {

fn try_put<Convert>(&self, v: Option<Convert>, s: &A) -> Option<A>
where
Arc<B>: From<Convert>,
Convert: Shared<B>,
{
Some((self.put)(s, From::from(v.unwrap())))
Some((self.put)(s, v.unwrap().shared()))
}
}

Expand All @@ -197,9 +199,9 @@ impl<A, B> Lens for GeneralLens<A, B> {

fn put<Convert>(&self, v: Convert, s: &A) -> A
where
Arc<B>: From<Convert>,
Convert: Shared<B>,
{
(self.put)(s, From::from(v))
(self.put)(s, v.shared())
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub mod list;
pub mod queue;
pub mod iter;
pub mod lens;
pub mod shared;

pub use conslist::ConsList;
pub use map::Map;
Expand Down
33 changes: 17 additions & 16 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::hash::{Hash, Hasher};
use std::fmt::{Debug, Formatter, Error};
use std::borrow::Borrow;
use queue::Queue;
use shared::Shared;

use self::ListNode::{Nil, Cons};

Expand Down Expand Up @@ -104,7 +105,7 @@ macro_rules! list {
/// untrained ear.
pub fn cons<A, RA, RD>(car: RA, cdr: RD) -> List<A>
where
Arc<A>: From<RA>,
RA: Shared<A>,
RD: Borrow<List<A>>,
{
cdr.borrow().cons(car)
Expand Down Expand Up @@ -138,7 +139,7 @@ impl<A> List<A> {
/// Construct a list with a single value.
pub fn singleton<R>(a: R) -> Self
where
Arc<A>: From<R>,
R: Shared<A>,
{
List::new().push_front(a)
}
Expand All @@ -165,9 +166,9 @@ impl<A> List<A> {
pub fn from<R, I>(it: I) -> List<A>
where
I: IntoIterator<Item = R>,
Arc<A>: From<R>,
R: Shared<A>,
{
it.into_iter().map(|a| Arc::from(a)).collect()
it.into_iter().map(|a| a.shared()).collect()
}

/// Test whether a list is empty.
Expand Down Expand Up @@ -269,16 +270,16 @@ impl<A> List<A> {
/// current list.
pub fn cons<R>(&self, a: R) -> Self
where
Arc<A>: From<R>,
R: Shared<A>,
{
List(Arc::new(Cons(1, Arc::from(a), Queue::new()))).append(self)
List(Arc::new(Cons(1, a.shared(), Queue::new()))).append(self)
}

/// Construct a list with a new value prepended to the front of the
/// current list.
pub fn push_front<R>(&self, a: R) -> Self
where
Arc<A>: From<R>,
R: Shared<A>,
{
self.cons(a)
}
Expand All @@ -292,16 +293,16 @@ impl<A> List<A> {
/// doubt did, this method is also available as `List::push_back()`.
pub fn snoc<R>(&self, a: R) -> Self
where
Arc<A>: From<R>,
R: Shared<A>,
{
self.append(&List(Arc::new(Cons(1, Arc::from(a), Queue::new()))))
self.append(&List(Arc::new(Cons(1, a.shared(), Queue::new()))))
}

/// Construct a list with a new value appended to the back of the
/// current list.
pub fn push_back<R>(&self, a: R) -> Self
where
Arc<A>: From<R>,
R: Shared<A>,
{
self.snoc(a)
}
Expand Down Expand Up @@ -522,9 +523,9 @@ impl<A: Ord> List<A> {
/// ```
pub fn insert<T>(&self, item: T) -> Self
where
Arc<A>: From<T>,
T: Shared<A>,
{
self.insert_ref(Arc::from(item))
self.insert_ref(item.shared())
}

fn insert_ref(&self, item: Arc<A>) -> Self {
Expand Down Expand Up @@ -698,7 +699,7 @@ impl<A> Sum for List<A> {

impl<A, T> FromIterator<T> for List<A>
where
Arc<A>: From<T>,
T: Shared<A>,
{
fn from_iter<I>(source: I) -> Self
where
Expand All @@ -712,7 +713,7 @@ where

impl<'a, A, T> From<&'a [T]> for List<A>
where
Arc<A>: From<&'a T>,
&'a T: Shared<A>,
{
fn from(slice: &'a [T]) -> List<A> {
slice.into_iter().collect()
Expand All @@ -721,7 +722,7 @@ where

impl<A, T> From<Vec<T>> for List<A>
where
Arc<A>: From<T>,
T: Shared<A>,
{
fn from(vec: Vec<T>) -> List<A> {
vec.into_iter().collect()
Expand All @@ -730,7 +731,7 @@ where

impl<'a, A, T> From<&'a Vec<T>> for List<A>
where
Arc<A>: From<&'a T>,
&'a T: Shared<A>,
{
fn from(vec: &'a Vec<T>) -> List<A> {
vec.into_iter().collect()
Expand Down
Loading