Skip to content

Commit ab5042b

Browse files
authored
Merge pull request #2 from KodrAus/fix/nightly
Add Shared type to fix inference fail on nightly
2 parents 5a48a65 + cc49e87 commit ab5042b

File tree

8 files changed

+126
-98
lines changed

8 files changed

+126
-98
lines changed

src/conslist.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::ops::Deref;
2626
use std::hash::{Hash, Hasher};
2727
use std::cmp::Ordering;
2828
use std::borrow::Borrow;
29+
use shared::Shared;
2930

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

@@ -113,7 +114,7 @@ macro_rules! conslist {
113114
/// untrained ear.
114115
pub fn cons<A, RA, RD>(car: RA, cdr: RD) -> ConsList<A>
115116
where
116-
Arc<A>: From<RA>,
117+
RA: Shared<A>,
117118
RD: Borrow<ConsList<A>>,
118119
{
119120
cdr.borrow().cons(car)
@@ -149,9 +150,9 @@ impl<A> ConsList<A> {
149150
/// Construct a list with a single element.
150151
pub fn singleton<R>(v: R) -> ConsList<A>
151152
where
152-
Arc<A>: From<R>,
153+
R: Shared<A>,
153154
{
154-
ConsList(Arc::new(Cons(1, Arc::from(v), conslist![])))
155+
ConsList(Arc::new(Cons(1, v.shared(), conslist![])))
155156
}
156157

157158
/// Construct a list by consuming an `IntoIterator`.
@@ -176,9 +177,9 @@ impl<A> ConsList<A> {
176177
pub fn from<R, I>(it: I) -> ConsList<A>
177178
where
178179
I: IntoIterator<Item = R>,
179-
Arc<A>: From<R>,
180+
R: Shared<A>,
180181
{
181-
it.into_iter().map(|a| Arc::from(a)).collect()
182+
it.into_iter().map(|a| a.shared()).collect()
182183
}
183184

184185
/// Test whether a list is empty.
@@ -197,9 +198,9 @@ impl<A> ConsList<A> {
197198
/// Time: O(1)
198199
pub fn cons<R>(&self, car: R) -> ConsList<A>
199200
where
200-
Arc<A>: From<R>,
201+
R: Shared<A>,
201202
{
202-
ConsList(Arc::new(Cons(self.len() + 1, Arc::from(car), self.clone())))
203+
ConsList(Arc::new(Cons(self.len() + 1, car.shared(), self.clone())))
203204
}
204205

205206
/// Get the first element of a list.
@@ -491,9 +492,9 @@ where
491492
/// ```
492493
pub fn insert<T>(&self, item: T) -> ConsList<A>
493494
where
494-
Arc<A>: From<T>,
495+
T: Shared<A>,
495496
{
496-
self.insert_ref(Arc::from(item))
497+
self.insert_ref(item.shared())
497498
}
498499

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

710711
impl<A, T> FromIterator<T> for ConsList<A>
711712
where
712-
Arc<A>: From<T>,
713+
T: Shared<A>,
713714
{
714715
fn from_iter<I>(source: I) -> Self
715716
where
@@ -726,28 +727,28 @@ where
726727

727728
impl<'a, A, R> From<&'a [R]> for ConsList<A>
728729
where
729-
Arc<A>: From<&'a R>,
730+
&'a R: Shared<A>,
730731
{
731732
fn from(slice: &'a [R]) -> Self {
732-
slice.into_iter().map(|a| Arc::from(a)).collect()
733+
slice.into_iter().map(|a| a.shared()).collect()
733734
}
734735
}
735736

736737
impl<A, R> From<Vec<R>> for ConsList<A>
737738
where
738-
Arc<A>: From<R>,
739+
R: Shared<A>,
739740
{
740741
fn from(vec: Vec<R>) -> Self {
741-
vec.into_iter().map(|a| Arc::from(a)).collect()
742+
vec.into_iter().map(|a| a.shared()).collect()
742743
}
743744
}
744745

745746
impl<'a, A, R> From<&'a Vec<R>> for ConsList<A>
746747
where
747-
Arc<A>: From<&'a R>,
748+
&'a R: Shared<A>,
748749
{
749750
fn from(vec: &'a Vec<R>) -> Self {
750-
vec.into_iter().map(|a| Arc::from(a)).collect()
751+
vec.into_iter().map(|a| a.shared()).collect()
751752
}
752753
}
753754

src/lens.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use std::marker::PhantomData;
1919
use std::sync::Arc;
2020

21+
use shared::Shared;
22+
2123
/// A lens from `From` to `To` where the focus of the lens isn't guaranteed to
2224
/// exist inside `From`. Operations on these lenses therefore return `Option`s.
2325
pub trait PartialLens: Clone {
@@ -31,7 +33,7 @@ pub trait PartialLens: Clone {
3133
/// operation succeeded.
3234
fn try_put<Convert>(&self, v: Option<Convert>, s: &Self::From) -> Option<Self::From>
3335
where
34-
Arc<Self::To>: From<Convert>;
36+
Convert: Shared<Self::To>;
3537

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

118120
fn try_put<FromC>(&self, v: Option<FromC>, s: &A) -> Option<A>
119121
where
120-
Arc<C>: From<FromC>,
122+
FromC: Shared<C>,
121123
{
122124
self.left
123125
.try_get(&s)
@@ -184,9 +186,9 @@ impl<A, B> PartialLens for GeneralLens<A, B> {
184186

185187
fn try_put<Convert>(&self, v: Option<Convert>, s: &A) -> Option<A>
186188
where
187-
Arc<B>: From<Convert>,
189+
Convert: Shared<B>,
188190
{
189-
Some((self.put)(s, From::from(v.unwrap())))
191+
Some((self.put)(s, v.unwrap().shared()))
190192
}
191193
}
192194

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

198200
fn put<Convert>(&self, v: Convert, s: &A) -> A
199201
where
200-
Arc<B>: From<Convert>,
202+
Convert: Shared<B>,
201203
{
202-
(self.put)(s, From::from(v))
204+
(self.put)(s, v.shared())
203205
}
204206
}
205207

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod list;
7676
pub mod queue;
7777
pub mod iter;
7878
pub mod lens;
79+
pub mod shared;
7980

8081
pub use conslist::ConsList;
8182
pub use map::Map;

src/list.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::hash::{Hash, Hasher};
2020
use std::fmt::{Debug, Formatter, Error};
2121
use std::borrow::Borrow;
2222
use queue::Queue;
23+
use shared::Shared;
2324

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

@@ -104,7 +105,7 @@ macro_rules! list {
104105
/// untrained ear.
105106
pub fn cons<A, RA, RD>(car: RA, cdr: RD) -> List<A>
106107
where
107-
Arc<A>: From<RA>,
108+
RA: Shared<A>,
108109
RD: Borrow<List<A>>,
109110
{
110111
cdr.borrow().cons(car)
@@ -138,7 +139,7 @@ impl<A> List<A> {
138139
/// Construct a list with a single value.
139140
pub fn singleton<R>(a: R) -> Self
140141
where
141-
Arc<A>: From<R>,
142+
R: Shared<A>,
142143
{
143144
List::new().push_front(a)
144145
}
@@ -165,9 +166,9 @@ impl<A> List<A> {
165166
pub fn from<R, I>(it: I) -> List<A>
166167
where
167168
I: IntoIterator<Item = R>,
168-
Arc<A>: From<R>,
169+
R: Shared<A>,
169170
{
170-
it.into_iter().map(|a| Arc::from(a)).collect()
171+
it.into_iter().map(|a| a.shared()).collect()
171172
}
172173

173174
/// Test whether a list is empty.
@@ -269,16 +270,16 @@ impl<A> List<A> {
269270
/// current list.
270271
pub fn cons<R>(&self, a: R) -> Self
271272
where
272-
Arc<A>: From<R>,
273+
R: Shared<A>,
273274
{
274-
List(Arc::new(Cons(1, Arc::from(a), Queue::new()))).append(self)
275+
List(Arc::new(Cons(1, a.shared(), Queue::new()))).append(self)
275276
}
276277

277278
/// Construct a list with a new value prepended to the front of the
278279
/// current list.
279280
pub fn push_front<R>(&self, a: R) -> Self
280281
where
281-
Arc<A>: From<R>,
282+
R: Shared<A>,
282283
{
283284
self.cons(a)
284285
}
@@ -292,16 +293,16 @@ impl<A> List<A> {
292293
/// doubt did, this method is also available as `List::push_back()`.
293294
pub fn snoc<R>(&self, a: R) -> Self
294295
where
295-
Arc<A>: From<R>,
296+
R: Shared<A>,
296297
{
297-
self.append(&List(Arc::new(Cons(1, Arc::from(a), Queue::new()))))
298+
self.append(&List(Arc::new(Cons(1, a.shared(), Queue::new()))))
298299
}
299300

300301
/// Construct a list with a new value appended to the back of the
301302
/// current list.
302303
pub fn push_back<R>(&self, a: R) -> Self
303304
where
304-
Arc<A>: From<R>,
305+
R: Shared<A>,
305306
{
306307
self.snoc(a)
307308
}
@@ -522,9 +523,9 @@ impl<A: Ord> List<A> {
522523
/// ```
523524
pub fn insert<T>(&self, item: T) -> Self
524525
where
525-
Arc<A>: From<T>,
526+
T: Shared<A>,
526527
{
527-
self.insert_ref(Arc::from(item))
528+
self.insert_ref(item.shared())
528529
}
529530

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

699700
impl<A, T> FromIterator<T> for List<A>
700701
where
701-
Arc<A>: From<T>,
702+
T: Shared<A>,
702703
{
703704
fn from_iter<I>(source: I) -> Self
704705
where
@@ -712,7 +713,7 @@ where
712713

713714
impl<'a, A, T> From<&'a [T]> for List<A>
714715
where
715-
Arc<A>: From<&'a T>,
716+
&'a T: Shared<A>,
716717
{
717718
fn from(slice: &'a [T]) -> List<A> {
718719
slice.into_iter().collect()
@@ -721,7 +722,7 @@ where
721722

722723
impl<A, T> From<Vec<T>> for List<A>
723724
where
724-
Arc<A>: From<T>,
725+
T: Shared<A>,
725726
{
726727
fn from(vec: Vec<T>) -> List<A> {
727728
vec.into_iter().collect()
@@ -730,7 +731,7 @@ where
730731

731732
impl<'a, A, T> From<&'a Vec<T>> for List<A>
732733
where
733-
Arc<A>: From<&'a T>,
734+
&'a T: Shared<A>,
734735
{
735736
fn from(vec: &'a Vec<T>) -> List<A> {
736737
vec.into_iter().collect()

0 commit comments

Comments
 (0)