Skip to content

Commit

Permalink
test_tools : rid of cyclic dependcy wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Nov 5, 2024
1 parent 821daf1 commit 29263cc
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 39 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,9 @@ version = "~1.0"
version = "~1.0"
# features = []
# default-features = false

[workspace.dependencies.hashbrown]
version = "~0.14.3"
# optional = true
# default-features = false
# features = [ "default" ]
5 changes: 2 additions & 3 deletions module/core/collection_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ all-features = false
[features]

no_std = [
# "test_tools/no_std",
]

use_alloc = [
"no_std", # qqq : use only use_alloc, eliminate feature no_std
"no_std",
"hashbrown",
]

Expand Down Expand Up @@ -61,7 +60,7 @@ collection_into_constructors = []
[dependencies]

## external
hashbrown = { version = "~0.14.3", optional = true, default-features = false, features = [ "default" ] }
hashbrown = { workspace = true, optional = true, default-features = false, features = [ "default" ] }

[dev-dependencies]
test_tools = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions module/core/collection_tools/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ assert_eq!( meta_list, meta_list );

### Basic Use Case :: `no_std` `HashSet` / `HashMap`

When implementing a `no_std` environment with the `use_alloc` feature in your Rust project, you'll encounter a challenge: collections like `Vec` are imported differently depending on the availability of the `std` library. Moreover, to use data structures such as `HashSet` or `HashMap` in a `no_std` context, it's necessary to depend on third-party crates, as these are not provided by the `alloc` crate directly. This crate aims to simplify the process of designing Rust libraries or applications that require these collections in a `no_std` environment, offering a more streamlined approach to working with dynamic data structures without the standard library.
When implementing a `no_std` ( `!use_std` ) environment with the `use_alloc` feature in your Rust project, you'll encounter a challenge: collections like `Vec` are imported differently depending on the availability of the `std` library. Moreover, to use data structures such as `HashSet` or `HashMap` in a `no_std` context, it's necessary to depend on third-party crates, as these are not provided by the `alloc` crate directly. This crate aims to simplify the process of designing Rust libraries or applications that require these collections in a `no_std` environment, offering a more streamlined approach to working with dynamic data structures without the standard library.

You can do

Expand All @@ -98,7 +98,7 @@ Instead of
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {

#[ cfg( feature = "use_alloc" ) ]
#[ cfg( all( feature = "no_std", feature = "use_alloc" ) ) ]
use hashbrown::HashSet; // a `no_std` replacement for `HashSet`
#[ cfg( not( feature = "no_std" ) ) ]
use std::collections::HashSet;
Expand All @@ -120,7 +120,8 @@ While strict macros require you to have all members of the same type, more relax

For example:
```rust
# #[ cfg( all( feature = "enabled", feature = "collection_into_constructors", any( not( feature = "no_std" ), feature = "use_alloc" ) ) ) ]
# #[ cfg( all( feature = "enabled", feature = "collection_into_constructors" ) ) ]
# #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
# {
use std::borrow::Cow;
let vec : Vec< String > = collection_tools::into_vec!( "&str", "String".to_string(), Cow::from( "Cow" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@
//! a `HashMap`, making your code cleaner and more concise. This is particularly useful in cases
//! where you need to define a map with a known set of key-value pairs upfront.
#[ cfg( not( all
(
// not( feature = "use_alloc" ) ) ],
all( feature = "enabled", feature = "collection_constructors" ),
any( not( feature = "no_std" ), feature = "use_alloc" )
#[ cfg( not( all(
feature = "enabled",
feature = "collection_constructors",
any( feature = "use_alloc", not( feature = "no_std" ) )
)))]
fn main(){}
fn main() {}

// zzz : aaa : rid of `#[ cfg( not( feature = "use_alloc" ) ) ]` -- Rid of by not relying on std
// #[ cfg( not( feature = "use_alloc" ) ) ]
#[ cfg( all( feature = "enabled", feature = "collection_constructors" ) ) ]
#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ]
#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
fn main()
{
use collection_tools::*;
Expand Down
5 changes: 4 additions & 1 deletion module/core/collection_tools/src/collection/binary_heap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[ allow( unused_imports ) ]
use super::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use alloc::collections::binary_heap::*;
pub use alloc::collections::binary_heap::*;

/// Creates a `BinaryHeap` from a list of elements.
///
Expand Down
3 changes: 3 additions & 0 deletions module/core/collection_tools/src/collection/btree_map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[ allow( unused_imports ) ]
use super::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use alloc::collections::btree_map::*;
Expand Down
3 changes: 3 additions & 0 deletions module/core/collection_tools/src/collection/btree_set.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[ allow( unused_imports ) ]
use super::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use alloc::collections::btree_set::*;
Expand Down
11 changes: 8 additions & 3 deletions module/core/collection_tools/src/collection/hash_map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#[ cfg( feature = "use_alloc" ) ]
#[ allow( unused_imports ) ]
use super::*;

// xxx : qqq : wrong
#[ cfg( all( feature = "no_std", feature = "use_alloc" ) ) ]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use crate::dependency::hashbrown::hash_map::*;

#[ cfg( not( feature = "no_std" ) ) ]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
Expand All @@ -14,7 +19,7 @@ pub use std::collections::hash_map::*;
/// # Origin
///
/// This collection can be reexported from different crates:
/// - from `std`, if `no_std` flag if off
/// - from `std`, if `use_std` is on ( `no_std` flag if off )
/// - from `hashbrown`, if `use_alloc` flag if on
///
/// # Syntax
Expand Down Expand Up @@ -98,7 +103,7 @@ macro_rules! hmap
/// # Origin
///
/// This collection can be reexported from different crates:
/// - from `std`, if `no_std` flag if off
/// - from `std`, if `use_std` is on ( `no_std` flag if off )
/// - from `hashbrown`, if `use_alloc` flag if on
///
/// # Syntax
Expand Down
8 changes: 6 additions & 2 deletions module/core/collection_tools/src/collection/hash_set.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#[ allow( unused_imports ) ]
use super::*;

#[ cfg( feature = "use_alloc" ) ]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use crate::dependency::hashbrown::hash_set::*;

#[ cfg( not( feature = "no_std" ) ) ]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
Expand All @@ -14,7 +18,7 @@ pub use std::collections::hash_set::*;
/// # Origin
///
/// This collection can be reexported from different crates:
/// - from `std`, if `no_std` flag if off
/// - from `std`, if `use_std` is on ( `no_std` flag if off )
/// - from `hashbrown`, if `use_alloc` flag if on
///
/// # Syntax
Expand Down Expand Up @@ -98,7 +102,7 @@ macro_rules! hset
/// # Origin
///
/// This collection can be reexported from different crates:
/// - from `std`, if `no_std` flag if off
/// - from `std`, if `use_std` is on ( `no_std` flag if off )
/// - from `hashbrown`, if `use_alloc` flag if on
///
/// # Syntax
Expand Down
3 changes: 3 additions & 0 deletions module/core/collection_tools/src/collection/linked_list.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[ allow( unused_imports ) ]
use super::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use alloc::collections::linked_list::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ macro_rules! count
);
}

#[ cfg( feature = "enabled" ) ]
#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
extern crate alloc;

/// [std::collections::BTreeMap] macros
pub mod btree_map;
/// [std::collections::BTreeSet] macros
Expand Down Expand Up @@ -43,7 +47,7 @@ pub mod own
{
use super::*;

pub use super::super::collection::
pub use super::
{
btree_map,
btree_set,
Expand Down Expand Up @@ -84,7 +88,8 @@ pub mod exposed
pub use super::super::collection;

#[ doc( inline ) ]
#[ cfg( any( feature = "use_alloc", all( feature = "collection_constructors", not( feature = "no_std" ) ) ) ) ]
#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
#[ cfg( feature = "collection_constructors" ) ]
pub use crate::
{
vec as dlist,
Expand All @@ -97,7 +102,8 @@ pub mod exposed
};

#[ doc( inline ) ]
#[ cfg( any( feature = "use_alloc", all( feature = "collection_into_constructors", not( feature = "no_std" ) ) ) ) ]
#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
#[ cfg( feature = "collection_into_constructors" ) ]
pub use crate::
{
into_vec,
Expand Down
3 changes: 3 additions & 0 deletions module/core/collection_tools/src/collection/vec_deque.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[ allow( unused_imports ) ]
use super::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use alloc::collections::vec_deque::*;
Expand Down
4 changes: 4 additions & 0 deletions module/core/collection_tools/src/collection/vector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#[ allow( unused_imports ) ]
use super::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use alloc::vec::*;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use core::slice::{ Iter, IterMut };
Expand Down
10 changes: 5 additions & 5 deletions module/core/collection_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#![ doc( html_root_url = "https://docs.rs/collection_tools/latest/collection_tools/" ) ]
#![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "Readme.md" ) ) ]

#[ cfg( feature = "enabled" ) ]
#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ]
extern crate alloc;
// #[ cfg( feature = "enabled" ) ]
// #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
// extern crate alloc;

/// Module containing all collection macros
#[ cfg( feature = "enabled" ) ]
#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ]
#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
pub mod collection;

// #[ cfg( feature = "enabled" ) ]
// #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ]
// #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
// pub use collection::*;

/// Namespace with dependencies.
Expand Down
2 changes: 1 addition & 1 deletion module/core/collection_tools/tests/inc/vec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[ test ]
#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ]
#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ]
fn reexport()
{

Expand Down
41 changes: 32 additions & 9 deletions module/core/test_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ all-features = false
default = [
"enabled",
# "standalone",
"normal",
]
full = [ "default" ]
no_std = [
Expand Down Expand Up @@ -65,8 +66,27 @@ enabled = [
]
# nightly = [ "typing_tools/nightly" ]

standalone = [ "standalone_error_tools" ]
normal = [
"dep:error_tools",
"dep:collection_tools",
"dep:meta_tools",
"dep:mem_tools",
"dep:typing_tools",
"dep:diagnostics_tools",
"dep:process_tools",
]
standalone = [
"standalone_error_tools",
"standalone_collection_tools",

"dep:meta_tools",
"dep:mem_tools",
"dep:typing_tools",
"dep:diagnostics_tools",
"dep:process_tools",
]
standalone_error_tools = [ "dep:anyhow", "dep:thiserror", "error_typed", "error_untyped" ]
standalone_collection_tools = [ "dep:hashbrown" ]
error_typed = []
error_untyped = []

Expand All @@ -82,21 +102,24 @@ rand = "0.8.5"

## internal

error_tools = { workspace = true, features = [ "full" ] }
meta_tools = { workspace = true, features = [ "full" ] }
mem_tools = { workspace = true, features = [ "full" ] }
typing_tools = { workspace = true, features = [ "full" ] }
diagnostics_tools = { workspace = true, features = [ "full" ] }
process_tools = { workspace = true, features = [ "full" ] }
collection_tools = { workspace = true, features = [ "full" ] }
error_tools = { workspace = true, features = [ "full" ], optional = true }
collection_tools = { workspace = true, features = [ "full" ], optional = true }

meta_tools = { workspace = true, features = [ "full" ], optional = true }
mem_tools = { workspace = true, features = [ "full" ], optional = true }
typing_tools = { workspace = true, features = [ "full" ], optional = true }
diagnostics_tools = { workspace = true, features = [ "full" ], optional = true }
process_tools = { workspace = true, features = [ "full" ], optional = true }

# former_stable = { workspace = true, features = [ "full" ] }

## transient

# # error_tools
# error_tools
anyhow = { workspace = true, optional = true }
thiserror = { workspace = true, optional = true }
# collection_tools
hashbrown = { workspace = true, optional = true }

[build-dependencies]
rustc_version = "0.4"
4 changes: 4 additions & 0 deletions module/core/test_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ pub mod test;
#[ path = "../../../core/error_tools/src/error/mod.rs" ]
pub mod error_tools;

#[ cfg( feature = "standalone" ) ]
#[ path = "../../../core/collection_tools/src/collection/mod.rs" ]
pub mod collection_tools;

#[ cfg( feature = "enabled" ) ]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
Expand Down

0 comments on commit 29263cc

Please sign in to comment.