Skip to content
Closed
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
4 changes: 4 additions & 0 deletions valuable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ criterion = "0.3"
[[bench]]
name = "structable"
harness = false

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
12 changes: 12 additions & 0 deletions valuable/src/cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
macro_rules! feature {
(
#![$meta:meta]
$($item:item)*
) => {
$(
#[cfg($meta)]
#[cfg_attr(docsrs, doc(cfg($meta)))]
$item
)*
}
}
18 changes: 11 additions & 7 deletions valuable/src/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,17 @@ macro_rules! deref {
deref! {
&T,
&mut T,
#[cfg(feature = "alloc")]
alloc::boxed::Box<T>,
#[cfg(feature = "alloc")]
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
#[cfg(feature = "alloc")]
alloc::sync::Arc<T>,
}

feature! {
#![feature = "alloc"]

deref! {
alloc::boxed::Box<T>,
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
alloc::sync::Arc<T>,
}
}

static RESULT_VARIANTS: &[VariantDef<'static>] = &[
Expand Down
11 changes: 9 additions & 2 deletions valuable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,14 @@
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(feature = "alloc")]
extern crate alloc;

#[macro_use]
mod cfg;

mod enumerable;
pub use enumerable::{EnumDef, Enumerable, Variant, VariantDef};

Expand Down Expand Up @@ -133,5 +137,8 @@ pub use value::Value;
mod visit;
pub use visit::{visit, Visit};

#[cfg(feature = "derive")]
pub use valuable_derive::*;
feature! {
#![feature = "derive"]

pub use valuable_derive::*;
}
82 changes: 44 additions & 38 deletions valuable/src/listable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,6 @@ macro_rules! deref {
deref! {
&T,
&mut T,
#[cfg(feature = "alloc")]
alloc::boxed::Box<T>,
#[cfg(feature = "alloc")]
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
#[cfg(feature = "alloc")]
alloc::sync::Arc<T>,
}

macro_rules! slice {
Expand Down Expand Up @@ -161,16 +154,7 @@ macro_rules! slice {

slice! {
(T: Valuable) &'_ [T],
#[cfg(feature = "alloc")]
(T: Valuable) alloc::boxed::Box<[T]>,
#[cfg(feature = "alloc")]
(T: Valuable) alloc::rc::Rc<[T]>,
#[cfg(not(valuable_no_atomic_cas))]
#[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>,
}

macro_rules! collection {
Expand Down Expand Up @@ -204,34 +188,56 @@ macro_rules! collection {
};
}

collection! {
#[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>,
}
feature! {
#![feature = "alloc"]
use alloc::collections;

deref!{
alloc::boxed::Box<T>,
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
alloc::sync::Arc<T>,
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Valuable for alloc::collections::VecDeque<T> {
fn as_value(&self) -> Value<'_> {
Value::Listable(self as &dyn Listable)
slice! {
(T: Valuable) alloc::boxed::Box<[T]>,
(T: Valuable) alloc::rc::Rc<[T]>,
#[cfg(not(valuable_no_atomic_cas))]
(T: Valuable) alloc::sync::Arc<[T]>,
(T: Valuable) alloc::vec::Vec<T>,
}

fn visit(&self, visit: &mut dyn Visit) {
let (first, second) = self.as_slices();
T::visit_slice(first, visit);
T::visit_slice(second, visit);
collection! {
(T: Valuable) collections::LinkedList<T>,
(T: Valuable + Ord) collections::BinaryHeap<T>,
(T: Valuable + Ord) collections::BTreeSet<T>,
}


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

fn visit(&self, visit: &mut dyn Visit) {
let (first, second) = self.as_slices();
T::visit_slice(first, visit);
T::visit_slice(second, visit);
}
}

impl<T: Valuable> Listable for collections::VecDeque<T> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
}

#[cfg(feature = "alloc")]
impl<T: Valuable> Listable for alloc::collections::VecDeque<T> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
feature! {
#![feature = "std"]

collection! {
(T: Valuable + Eq + std::hash::Hash, H: std::hash::BuildHasher) std::collections::HashSet<T, H>,
}
}

Expand Down
68 changes: 36 additions & 32 deletions valuable/src/mappable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,52 +119,56 @@ macro_rules! deref {
deref! {
&T,
&mut T,
#[cfg(feature = "alloc")]
alloc::boxed::Box<T>,
#[cfg(feature = "alloc")]
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
#[cfg(feature = "alloc")]
alloc::sync::Arc<T>,
}

#[cfg(feature = "std")]
impl<K: Valuable, V: Valuable> Valuable for std::collections::HashMap<K, V> {
fn as_value(&self) -> Value<'_> {
Value::Mappable(self)
feature! {
#![feature = "alloc"]

deref! {
alloc::boxed::Box<T>,
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
alloc::sync::Arc<T>,
}
Comment on lines +124 to 132
Copy link
Copy Markdown
Member

@taiki-e taiki-e Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feature! macro doesn't seem to work on nested macro.

doc


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

impl<K: Valuable, V: Valuable> Valuable for alloc::collections::BTreeMap<K, V> {
fn as_value(&self) -> Value<'_> {
Value::Mappable(self)
}

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

#[cfg(feature = "std")]
impl<K: Valuable, V: Valuable> Mappable for std::collections::HashMap<K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter().size_hint()
impl<K: Valuable, V: Valuable> Mappable for alloc::collections::BTreeMap<K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter().size_hint()
}
}
}

#[cfg(feature = "alloc")]
impl<K: Valuable, V: Valuable> Valuable for alloc::collections::BTreeMap<K, V> {
fn as_value(&self) -> Value<'_> {
Value::Mappable(self)
}
feature! {
#![feature = "std"]
impl<K: Valuable, V: Valuable> Valuable for std::collections::HashMap<K, V> {
fn as_value(&self) -> Value<'_> {
Value::Mappable(self)
}

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

#[cfg(feature = "alloc")]
impl<K: Valuable, V: Valuable> Mappable for alloc::collections::BTreeMap<K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter().size_hint()
impl<K: Valuable, V: Valuable> Mappable for std::collections::HashMap<K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter().size_hint()
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions valuable/src/structable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,15 @@ macro_rules! deref {
deref! {
&T,
&mut T,
#[cfg(feature = "alloc")]
alloc::boxed::Box<T>,
#[cfg(feature = "alloc")]
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
#[cfg(feature = "alloc")]
alloc::sync::Arc<T>,
}

feature! {
#![feature = "alloc"]

deref! {
alloc::boxed::Box<T>,
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
alloc::sync::Arc<T>,
}
}
18 changes: 11 additions & 7 deletions valuable/src/tuplable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ macro_rules! deref {
deref! {
&T,
&mut T,
#[cfg(feature = "alloc")]
alloc::boxed::Box<T>,
#[cfg(feature = "alloc")]
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
#[cfg(feature = "alloc")]
alloc::sync::Arc<T>,
}

feature! {
#![feature = "alloc"]

deref!{
alloc::boxed::Box<T>,
alloc::rc::Rc<T>,
#[cfg(not(valuable_no_atomic_cas))]
alloc::sync::Arc<T>,
}
}

impl Tuplable for () {
Expand Down
Loading