From 29263cc67c3a3dcb82084c840ebba862ea79e815 Mon Sep 17 00:00:00 2001 From: wandalen Date: Tue, 5 Nov 2024 19:30:54 +0200 Subject: [PATCH] test_tools : rid of cyclic dependcy wip --- Cargo.toml | 6 +++ module/core/collection_tools/Cargo.toml | 5 +-- module/core/collection_tools/Readme.md | 7 ++-- .../examples/collection_tools_trivial.rs | 15 +++---- .../src/collection/binary_heap.rs | 5 ++- .../src/collection/btree_map.rs | 3 ++ .../src/collection/btree_set.rs | 3 ++ .../src/collection/hash_map.rs | 11 +++-- .../src/collection/hash_set.rs | 8 +++- .../src/collection/linked_list.rs | 3 ++ .../src/{collection.rs => collection/mod.rs} | 12 ++++-- .../src/collection/vec_deque.rs | 3 ++ .../collection_tools/src/collection/vector.rs | 4 ++ module/core/collection_tools/src/lib.rs | 10 ++--- module/core/collection_tools/tests/inc/vec.rs | 2 +- module/core/test_tools/Cargo.toml | 41 +++++++++++++++---- module/core/test_tools/src/lib.rs | 4 ++ 17 files changed, 103 insertions(+), 39 deletions(-) rename module/core/collection_tools/src/{collection.rs => collection/mod.rs} (88%) diff --git a/Cargo.toml b/Cargo.toml index 9c7de3db87..521c616254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" ] diff --git a/module/core/collection_tools/Cargo.toml b/module/core/collection_tools/Cargo.toml index 59c5bebc98..95e3268fc1 100644 --- a/module/core/collection_tools/Cargo.toml +++ b/module/core/collection_tools/Cargo.toml @@ -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", ] @@ -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 } diff --git a/module/core/collection_tools/Readme.md b/module/core/collection_tools/Readme.md index 1430c6d6ef..a991e3ae13 100644 --- a/module/core/collection_tools/Readme.md +++ b/module/core/collection_tools/Readme.md @@ -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 @@ -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; @@ -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" ) ); diff --git a/module/core/collection_tools/examples/collection_tools_trivial.rs b/module/core/collection_tools/examples/collection_tools_trivial.rs index 8a11bb85bf..79ff09bf0d 100644 --- a/module/core/collection_tools/examples/collection_tools_trivial.rs +++ b/module/core/collection_tools/examples/collection_tools_trivial.rs @@ -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::*; diff --git a/module/core/collection_tools/src/collection/binary_heap.rs b/module/core/collection_tools/src/collection/binary_heap.rs index 46f7609bd4..965f5804c5 100644 --- a/module/core/collection_tools/src/collection/binary_heap.rs +++ b/module/core/collection_tools/src/collection/binary_heap.rs @@ -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. /// diff --git a/module/core/collection_tools/src/collection/btree_map.rs b/module/core/collection_tools/src/collection/btree_map.rs index cee5f0a8bd..2e4ec94f13 100644 --- a/module/core/collection_tools/src/collection/btree_map.rs +++ b/module/core/collection_tools/src/collection/btree_map.rs @@ -1,3 +1,6 @@ +#[ allow( unused_imports ) ] +use super::*; + #[ doc( inline ) ] #[ allow( unused_imports ) ] pub use alloc::collections::btree_map::*; diff --git a/module/core/collection_tools/src/collection/btree_set.rs b/module/core/collection_tools/src/collection/btree_set.rs index f7eb344274..7111811c2e 100644 --- a/module/core/collection_tools/src/collection/btree_set.rs +++ b/module/core/collection_tools/src/collection/btree_set.rs @@ -1,3 +1,6 @@ +#[ allow( unused_imports ) ] +use super::*; + #[ doc( inline ) ] #[ allow( unused_imports ) ] pub use alloc::collections::btree_set::*; diff --git a/module/core/collection_tools/src/collection/hash_map.rs b/module/core/collection_tools/src/collection/hash_map.rs index 5f7eaf1526..8ebbc9f90f 100644 --- a/module/core/collection_tools/src/collection/hash_map.rs +++ b/module/core/collection_tools/src/collection/hash_map.rs @@ -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 ) ] @@ -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 @@ -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 diff --git a/module/core/collection_tools/src/collection/hash_set.rs b/module/core/collection_tools/src/collection/hash_set.rs index 21e2c0a6c7..6fe8d8287a 100644 --- a/module/core/collection_tools/src/collection/hash_set.rs +++ b/module/core/collection_tools/src/collection/hash_set.rs @@ -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 ) ] @@ -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 @@ -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 diff --git a/module/core/collection_tools/src/collection/linked_list.rs b/module/core/collection_tools/src/collection/linked_list.rs index 1ace24e4da..cc23637be1 100644 --- a/module/core/collection_tools/src/collection/linked_list.rs +++ b/module/core/collection_tools/src/collection/linked_list.rs @@ -1,3 +1,6 @@ +#[ allow( unused_imports ) ] +use super::*; + #[ doc( inline ) ] #[ allow( unused_imports ) ] pub use alloc::collections::linked_list::*; diff --git a/module/core/collection_tools/src/collection.rs b/module/core/collection_tools/src/collection/mod.rs similarity index 88% rename from module/core/collection_tools/src/collection.rs rename to module/core/collection_tools/src/collection/mod.rs index d6cdafdb2a..0f74158835 100644 --- a/module/core/collection_tools/src/collection.rs +++ b/module/core/collection_tools/src/collection/mod.rs @@ -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 @@ -43,7 +47,7 @@ pub mod own { use super::*; - pub use super::super::collection:: + pub use super:: { btree_map, btree_set, @@ -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, @@ -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, diff --git a/module/core/collection_tools/src/collection/vec_deque.rs b/module/core/collection_tools/src/collection/vec_deque.rs index ef418ea50b..1060d01c0b 100644 --- a/module/core/collection_tools/src/collection/vec_deque.rs +++ b/module/core/collection_tools/src/collection/vec_deque.rs @@ -1,3 +1,6 @@ +#[ allow( unused_imports ) ] +use super::*; + #[ doc( inline ) ] #[ allow( unused_imports ) ] pub use alloc::collections::vec_deque::*; diff --git a/module/core/collection_tools/src/collection/vector.rs b/module/core/collection_tools/src/collection/vector.rs index 6e024b4b61..0ae9ccf6dd 100644 --- a/module/core/collection_tools/src/collection/vector.rs +++ b/module/core/collection_tools/src/collection/vector.rs @@ -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 }; diff --git a/module/core/collection_tools/src/lib.rs b/module/core/collection_tools/src/lib.rs index d7a403088f..bcbbb4bdbc 100644 --- a/module/core/collection_tools/src/lib.rs +++ b/module/core/collection_tools/src/lib.rs @@ -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. diff --git a/module/core/collection_tools/tests/inc/vec.rs b/module/core/collection_tools/tests/inc/vec.rs index bb64690898..5bf78631ba 100644 --- a/module/core/collection_tools/tests/inc/vec.rs +++ b/module/core/collection_tools/tests/inc/vec.rs @@ -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() { diff --git a/module/core/test_tools/Cargo.toml b/module/core/test_tools/Cargo.toml index 4c11306030..2c1850c70c 100644 --- a/module/core/test_tools/Cargo.toml +++ b/module/core/test_tools/Cargo.toml @@ -30,6 +30,7 @@ all-features = false default = [ "enabled", # "standalone", + "normal", ] full = [ "default" ] no_std = [ @@ -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 = [] @@ -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" diff --git a/module/core/test_tools/src/lib.rs b/module/core/test_tools/src/lib.rs index 42af055957..ff58b9c6e9 100644 --- a/module/core/test_tools/src/lib.rs +++ b/module/core/test_tools/src/lib.rs @@ -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 ) ]