Skip to content

Commit fb27fcb

Browse files
authored
impl valuable traits for more types (#56)
1 parent ea22d23 commit fb27fcb

File tree

7 files changed

+207
-189
lines changed

7 files changed

+207
-189
lines changed

valuable/src/enumerable.rs

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -608,56 +608,36 @@ impl fmt::Debug for dyn Enumerable + '_ {
608608
}
609609
}
610610

611-
impl<E: ?Sized + Enumerable> Enumerable for &E {
612-
fn definition(&self) -> EnumDef<'_> {
613-
E::definition(*self)
614-
}
615-
616-
fn variant(&self) -> Variant<'_> {
617-
E::variant(*self)
618-
}
619-
}
620-
621-
impl<E: ?Sized + Enumerable> Enumerable for &mut E {
622-
fn definition(&self) -> EnumDef<'_> {
623-
E::definition(&**self)
624-
}
625-
626-
fn variant(&self) -> Variant<'_> {
627-
E::variant(&**self)
628-
}
629-
}
630-
631-
#[cfg(feature = "alloc")]
632-
impl<E: ?Sized + Enumerable> Enumerable for alloc::boxed::Box<E> {
633-
fn definition(&self) -> EnumDef<'_> {
634-
E::definition(&**self)
635-
}
636-
637-
fn variant(&self) -> Variant<'_> {
638-
E::variant(&**self)
639-
}
640-
}
641-
642-
#[cfg(feature = "alloc")]
643-
impl<E: ?Sized + Enumerable> Enumerable for alloc::rc::Rc<E> {
644-
fn definition(&self) -> EnumDef<'_> {
645-
E::definition(&**self)
646-
}
611+
macro_rules! deref {
612+
(
613+
$(
614+
$(#[$attrs:meta])*
615+
$ty:ty,
616+
)*
617+
) => {
618+
$(
619+
$(#[$attrs])*
620+
impl<T: ?Sized + Enumerable> Enumerable for $ty {
621+
fn definition(&self) -> EnumDef<'_> {
622+
T::definition(&**self)
623+
}
647624

648-
fn variant(&self) -> Variant<'_> {
649-
E::variant(&**self)
650-
}
625+
fn variant(&self) -> Variant<'_> {
626+
T::variant(&**self)
627+
}
628+
}
629+
)*
630+
};
651631
}
652632

653-
#[cfg(not(valuable_no_atomic_cas))]
654-
#[cfg(feature = "alloc")]
655-
impl<E: ?Sized + Enumerable> Enumerable for alloc::sync::Arc<E> {
656-
fn definition(&self) -> EnumDef<'_> {
657-
E::definition(&**self)
658-
}
659-
660-
fn variant(&self) -> Variant<'_> {
661-
E::variant(&**self)
662-
}
633+
deref! {
634+
&T,
635+
&mut T,
636+
#[cfg(feature = "alloc")]
637+
alloc::boxed::Box<T>,
638+
#[cfg(feature = "alloc")]
639+
alloc::rc::Rc<T>,
640+
#[cfg(not(valuable_no_atomic_cas))]
641+
#[cfg(feature = "alloc")]
642+
alloc::sync::Arc<T>,
663643
}

valuable/src/listable.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -100,38 +100,34 @@ pub trait Listable: Valuable {
100100
fn size_hint(&self) -> (usize, Option<usize>);
101101
}
102102

103-
impl<L: ?Sized + Listable> Listable for &L {
104-
fn size_hint(&self) -> (usize, Option<usize>) {
105-
L::size_hint(*self)
106-
}
107-
}
108-
109-
impl<L: ?Sized + Listable> Listable for &mut L {
110-
fn size_hint(&self) -> (usize, Option<usize>) {
111-
L::size_hint(&**self)
112-
}
113-
}
114-
115-
#[cfg(feature = "alloc")]
116-
impl<L: ?Sized + Listable> Listable for alloc::boxed::Box<L> {
117-
fn size_hint(&self) -> (usize, Option<usize>) {
118-
L::size_hint(&**self)
119-
}
120-
}
121-
122-
#[cfg(feature = "alloc")]
123-
impl<L: ?Sized + Listable> Listable for alloc::rc::Rc<L> {
124-
fn size_hint(&self) -> (usize, Option<usize>) {
125-
L::size_hint(&**self)
126-
}
103+
macro_rules! deref {
104+
(
105+
$(
106+
$(#[$attrs:meta])*
107+
$ty:ty,
108+
)*
109+
) => {
110+
$(
111+
$(#[$attrs])*
112+
impl<T: ?Sized + Listable> Listable for $ty {
113+
fn size_hint(&self) -> (usize, Option<usize>) {
114+
T::size_hint(&**self)
115+
}
116+
}
117+
)*
118+
};
127119
}
128120

129-
#[cfg(not(valuable_no_atomic_cas))]
130-
#[cfg(feature = "alloc")]
131-
impl<L: ?Sized + Listable> Listable for alloc::sync::Arc<L> {
132-
fn size_hint(&self) -> (usize, Option<usize>) {
133-
L::size_hint(&**self)
134-
}
121+
deref! {
122+
&T,
123+
&mut T,
124+
#[cfg(feature = "alloc")]
125+
alloc::boxed::Box<T>,
126+
#[cfg(feature = "alloc")]
127+
alloc::rc::Rc<T>,
128+
#[cfg(not(valuable_no_atomic_cas))]
129+
#[cfg(feature = "alloc")]
130+
alloc::sync::Arc<T>,
135131
}
136132

137133
macro_rules! slice {
@@ -214,7 +210,7 @@ collection! {
214210
#[cfg(feature = "alloc")]
215211
(T: Valuable + Ord) alloc::collections::BinaryHeap<T>,
216212
#[cfg(feature = "alloc")]
217-
(T: Valuable + Ord) alloc::collections::BTreeSet<T> ,
213+
(T: Valuable + Ord) alloc::collections::BTreeSet<T>,
218214
#[cfg(feature = "std")]
219215
(T: Valuable + Eq + std::hash::Hash, H: std::hash::BuildHasher) std::collections::HashSet<T, H>,
220216
}

valuable/src/mappable.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,38 +98,34 @@ pub trait Mappable: Valuable {
9898
fn size_hint(&self) -> (usize, Option<usize>);
9999
}
100100

101-
impl<M: ?Sized + Mappable> Mappable for &M {
102-
fn size_hint(&self) -> (usize, Option<usize>) {
103-
M::size_hint(*self)
104-
}
105-
}
106-
107-
impl<M: ?Sized + Mappable> Mappable for &mut M {
108-
fn size_hint(&self) -> (usize, Option<usize>) {
109-
M::size_hint(&**self)
110-
}
111-
}
112-
113-
#[cfg(feature = "alloc")]
114-
impl<M: ?Sized + Mappable> Mappable for alloc::boxed::Box<M> {
115-
fn size_hint(&self) -> (usize, Option<usize>) {
116-
M::size_hint(&**self)
117-
}
118-
}
119-
120-
#[cfg(feature = "alloc")]
121-
impl<M: ?Sized + Mappable> Mappable for alloc::rc::Rc<M> {
122-
fn size_hint(&self) -> (usize, Option<usize>) {
123-
M::size_hint(&**self)
124-
}
101+
macro_rules! deref {
102+
(
103+
$(
104+
$(#[$attrs:meta])*
105+
$ty:ty,
106+
)*
107+
) => {
108+
$(
109+
$(#[$attrs])*
110+
impl<T: ?Sized + Mappable> Mappable for $ty {
111+
fn size_hint(&self) -> (usize, Option<usize>) {
112+
T::size_hint(&**self)
113+
}
114+
}
115+
)*
116+
};
125117
}
126118

127-
#[cfg(not(valuable_no_atomic_cas))]
128-
#[cfg(feature = "alloc")]
129-
impl<M: ?Sized + Mappable> Mappable for alloc::sync::Arc<M> {
130-
fn size_hint(&self) -> (usize, Option<usize>) {
131-
M::size_hint(&**self)
132-
}
119+
deref! {
120+
&T,
121+
&mut T,
122+
#[cfg(feature = "alloc")]
123+
alloc::boxed::Box<T>,
124+
#[cfg(feature = "alloc")]
125+
alloc::rc::Rc<T>,
126+
#[cfg(not(valuable_no_atomic_cas))]
127+
#[cfg(feature = "alloc")]
128+
alloc::sync::Arc<T>,
133129
}
134130

135131
#[cfg(feature = "std")]

valuable/src/structable.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -465,36 +465,32 @@ impl<'a> StructDef<'a> {
465465
}
466466
}
467467

468-
impl<S: ?Sized + Structable> Structable for &S {
469-
fn definition(&self) -> StructDef<'_> {
470-
S::definition(*self)
471-
}
472-
}
473-
474-
impl<S: ?Sized + Structable> Structable for &mut S {
475-
fn definition(&self) -> StructDef<'_> {
476-
S::definition(&**self)
477-
}
478-
}
479-
480-
#[cfg(feature = "alloc")]
481-
impl<S: ?Sized + Structable> Structable for alloc::boxed::Box<S> {
482-
fn definition(&self) -> StructDef<'_> {
483-
S::definition(&**self)
484-
}
485-
}
486-
487-
#[cfg(feature = "alloc")]
488-
impl<S: ?Sized + Structable> Structable for alloc::rc::Rc<S> {
489-
fn definition(&self) -> StructDef<'_> {
490-
S::definition(&**self)
491-
}
468+
macro_rules! deref {
469+
(
470+
$(
471+
$(#[$attrs:meta])*
472+
$ty:ty,
473+
)*
474+
) => {
475+
$(
476+
$(#[$attrs])*
477+
impl<T: ?Sized + Structable> Structable for $ty {
478+
fn definition(&self) -> StructDef<'_> {
479+
T::definition(&**self)
480+
}
481+
}
482+
)*
483+
};
492484
}
493485

494-
#[cfg(not(valuable_no_atomic_cas))]
495-
#[cfg(feature = "alloc")]
496-
impl<S: ?Sized + Structable> Structable for alloc::sync::Arc<S> {
497-
fn definition(&self) -> StructDef<'_> {
498-
S::definition(&**self)
499-
}
486+
deref! {
487+
&T,
488+
&mut T,
489+
#[cfg(feature = "alloc")]
490+
alloc::boxed::Box<T>,
491+
#[cfg(feature = "alloc")]
492+
alloc::rc::Rc<T>,
493+
#[cfg(not(valuable_no_atomic_cas))]
494+
#[cfg(feature = "alloc")]
495+
alloc::sync::Arc<T>,
500496
}

valuable/src/tuplable.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,36 @@ pub enum TupleDef {
108108
},
109109
}
110110

111+
macro_rules! deref {
112+
(
113+
$(
114+
$(#[$attrs:meta])*
115+
$ty:ty,
116+
)*
117+
) => {
118+
$(
119+
$(#[$attrs])*
120+
impl<T: ?Sized + Tuplable> Tuplable for $ty {
121+
fn definition(&self) -> TupleDef {
122+
T::definition(&**self)
123+
}
124+
}
125+
)*
126+
};
127+
}
128+
129+
deref! {
130+
&T,
131+
&mut T,
132+
#[cfg(feature = "alloc")]
133+
alloc::boxed::Box<T>,
134+
#[cfg(feature = "alloc")]
135+
alloc::rc::Rc<T>,
136+
#[cfg(not(valuable_no_atomic_cas))]
137+
#[cfg(feature = "alloc")]
138+
alloc::sync::Arc<T>,
139+
}
140+
111141
impl Tuplable for () {
112142
fn definition(&self) -> TupleDef {
113143
TupleDef::Static { fields: 0 }

0 commit comments

Comments
 (0)