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

impl Listable for std collections #13

Merged
merged 2 commits into from
May 11, 2021
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
96 changes: 78 additions & 18 deletions valuable/src/listable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,90 @@ impl<L: ?Sized + Listable> Listable for alloc::sync::Arc<L> {
}
}

impl<T: Valuable> Listable for &'_ [T] {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
macro_rules! slice {
(
$(
$(#[$attrs:meta])*
($($generics:tt)*) $ty:ty,
)*
) => {
$(
$(#[$attrs])*
impl<$($generics)*> Valuable for $ty {
fn as_value(&self) -> Value<'_> {
Value::Listable(self as &dyn Listable)
}

fn visit(&self, visit: &mut dyn Visit) {
T::visit_slice(self, visit);
}
}

$(#[$attrs])*
impl<$($generics)*> Listable for $ty {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
)*
};
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Listable for alloc::boxed::Box<[T]> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
slice! {
(T: Valuable) &'_ [T],
#[cfg(feature = "alloc")]
(T: Valuable) alloc::boxed::Box<[T]>,
#[cfg(feature = "alloc")]
(T: Valuable) alloc::rc::Rc<[T]>,
#[cfg(feature = "alloc")]
(T: Valuable) alloc::sync::Arc<[T]>,
(T: Valuable, const N: usize) [T; N],
#[cfg(feature = "alloc")]
(T: Valuable) alloc::vec::Vec<T>,
}

impl<T: Valuable, const N: usize> Listable for [T; N] {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
macro_rules! collection {
(
$(
$(#[$attrs:meta])*
($($generics:tt)*) $ty:ty,
)*
) => {
$(
$(#[$attrs])*
impl<$($generics)*> Valuable for $ty {
fn as_value(&self) -> Value<'_> {
Value::Listable(self as &dyn Listable)
}

fn visit(&self, visit: &mut dyn Visit) {
for value in self.iter() {
visit.visit_value(value.as_value());
}
}
}

$(#[$attrs])*
impl<$($generics)*> Listable for $ty {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
)*
};
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Listable for alloc::vec::Vec<T> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
collection! {
#[cfg(feature = "alloc")]
(T: Valuable) alloc::collections::VecDeque<T>,
#[cfg(feature = "alloc")]
(T: Valuable) alloc::collections::LinkedList<T>,
#[cfg(feature = "alloc")]
(T: Valuable + Ord) alloc::collections::BinaryHeap<T>,
#[cfg(feature = "alloc")]
(T: Valuable + Ord) alloc::collections::BTreeSet<T> ,
#[cfg(feature = "std")]
(T: Valuable + Eq + std::hash::Hash, H: std::hash::BuildHasher) std::collections::HashSet<T, H>,
}

impl fmt::Debug for dyn Listable + '_ {
Expand Down
44 changes: 1 addition & 43 deletions valuable/src/valuable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Listable, Slice, Value, Visit};
use crate::{Slice, Value, Visit};

use core::fmt;

Expand Down Expand Up @@ -179,48 +179,6 @@ impl Valuable for alloc::string::String {
}
}

impl<T: Valuable> Valuable for &'_ [T] {
fn as_value(&self) -> Value<'_> {
Value::Listable(self as &dyn Listable)
}

fn visit(&self, visit: &mut dyn Visit) {
T::visit_slice(self, visit);
}
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Valuable for alloc::boxed::Box<[T]> {
fn as_value(&self) -> Value<'_> {
Value::Listable(self as &dyn Listable)
}

fn visit(&self, visit: &mut dyn Visit) {
T::visit_slice(self, visit);
}
}

impl<T: Valuable, const N: usize> Valuable for [T; N] {
fn as_value(&self) -> Value<'_> {
Value::Listable(self)
}

fn visit(&self, visit: &mut dyn Visit) {
T::visit_slice(self, visit);
}
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Valuable for alloc::vec::Vec<T> {
fn as_value(&self) -> Value<'_> {
Value::Listable(self)
}

fn visit(&self, visit: &mut dyn Visit) {
T::visit_slice(self, visit);
}
}

#[cfg(feature = "std")]
impl Valuable for dyn std::error::Error + '_ {
fn as_value(&self) -> Value<'_> {
Expand Down