From bccd685888ba72567705720e6c5160ec6ce9d292 Mon Sep 17 00:00:00 2001 From: Anton Parfonov Date: Tue, 14 May 2024 11:57:47 +0300 Subject: [PATCH 1/2] Rename prelude to reexport, reorganize reexports --- module/core/collection_tools/Cargo.toml | 10 +-- module/core/collection_tools/src/lib.rs | 81 ++++++++----------- .../core/collection_tools/tests/inc/bmap.rs | 2 +- .../core/collection_tools/tests/inc/bset.rs | 2 +- .../collection_tools/tests/inc/components.rs | 51 ------------ .../core/collection_tools/tests/inc/heap.rs | 2 +- .../core/collection_tools/tests/inc/hmap.rs | 14 ++-- .../core/collection_tools/tests/inc/hset.rs | 12 +-- .../core/collection_tools/tests/inc/list.rs | 2 +- module/core/collection_tools/tests/inc/vec.rs | 79 +++++++++++++++--- .../core/collection_tools/tests/inc/vecd.rs | 2 +- module/core/data_type/Cargo.toml | 2 +- 12 files changed, 129 insertions(+), 130 deletions(-) diff --git a/module/core/collection_tools/Cargo.toml b/module/core/collection_tools/Cargo.toml index aecaca11b0..f5b9a2ca45 100644 --- a/module/core/collection_tools/Cargo.toml +++ b/module/core/collection_tools/Cargo.toml @@ -32,27 +32,27 @@ no_std = [ ] use_alloc = [ - "no_std", # qqq : for Anton : why is that better? + "no_std", # qqq : for Anton : why is that better? -- use_alloc means that we do not use std, but alloc and hashbrown "hashbrown", - # "test_tools/use_alloc", // why is it needed? + # "test_tools/use_alloc", // why is it needed? -- not needed, removed ] default = [ "enabled", - "prelude", + "reexports", "collection_constructors", "collection_into_constructors", ] full = [ "enabled", - "prelude", + "reexports", "collection_constructors", "collection_into_constructors", ] enabled = [] -prelude = [] +reexports = [] # Collection constructors, like `hmap!{ "key" => "val" }` collection_constructors = [] diff --git a/module/core/collection_tools/src/lib.rs b/module/core/collection_tools/src/lib.rs index 7564144d2c..c2faa8d516 100644 --- a/module/core/collection_tools/src/lib.rs +++ b/module/core/collection_tools/src/lib.rs @@ -36,27 +36,6 @@ pub mod protected #[ allow( unused_imports ) ] pub use super::orphan::*; - extern crate alloc; - - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use alloc::vec::Vec; - - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use alloc::collections::{ BinaryHeap, BTreeMap, BTreeSet, LinkedList, VecDeque }; - - // qqq : what is comnination `use_alloc` + !`no_std` - #[ cfg( feature = "use_alloc" ) ] - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use super::dependency::hashbrown::{ HashMap, HashSet }; - - #[ cfg( not( feature = "no_std" ) ) ] - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use std::collections::{ HashMap, HashSet }; - } /// Parented namespace of the module. @@ -82,30 +61,40 @@ pub mod exposed pub mod prelude { - // qqq : for Anton : uncomment, make it working and cover by tests - // #[ cfg( feature = "prelude" ) ] - // #[ doc( inline ) ] - // #[ allow( unused_imports ) ] - // pub use crate:: - // { - // HashMap as Map, - // HashSet as Set, - // HashMap, - // HashSet, - // VecDeque, - // BTreeMap, - // BTreeSet, - // BinaryHeap, - // LinkedList, - // }; - - // #[ cfg( feature = "prelude" ) ] - // #[ doc( inline ) ] - // #[ allow( unused_imports ) ] - // pub use crate:: - // { - // Vec, - // Vec as DynArray, - // }; + // qqq : for Anton : uncomment, make it working and cover by tests -- renamed to reexports + extern crate alloc; + + pub use alloc::collections; + + pub use alloc::vec; + + #[ cfg( feature = "use_alloc" ) ] + #[ doc( inline ) ] + #[ allow( unused_imports ) ] + pub use super::dependency::hashbrown as hash; + + #[ cfg( not( feature = "no_std" ) ) ] + #[ doc( inline ) ] + #[ allow( unused_imports ) ] + pub use std::collections as hash; + #[ cfg( feature = "reexports" ) ] + #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] + #[ doc( inline ) ] + #[ allow( unused_imports ) ] + pub use + { + collections::BTreeMap, + collections::BTreeSet, + collections::BinaryHeap, + hash::HashMap, + hash::HashSet, + collections::LinkedList, + vec::Vec, + collections::VecDeque, + + HashMap as Map, + HashSet as Set, + Vec as DynArray, + }; } diff --git a/module/core/collection_tools/tests/inc/bmap.rs b/module/core/collection_tools/tests/inc/bmap.rs index 86beb642c3..2fc68dd46b 100644 --- a/module/core/collection_tools/tests/inc/bmap.rs +++ b/module/core/collection_tools/tests/inc/bmap.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { diff --git a/module/core/collection_tools/tests/inc/bset.rs b/module/core/collection_tools/tests/inc/bset.rs index 70b7e56144..00acb093bd 100644 --- a/module/core/collection_tools/tests/inc/bset.rs +++ b/module/core/collection_tools/tests/inc/bset.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { diff --git a/module/core/collection_tools/tests/inc/components.rs b/module/core/collection_tools/tests/inc/components.rs index ee19c86a6c..e2503addb7 100644 --- a/module/core/collection_tools/tests/inc/components.rs +++ b/module/core/collection_tools/tests/inc/components.rs @@ -1,55 +1,4 @@ #[ allow( unused_imports ) ] use super::*; -// - -// qqq : implement similar test for all containers -#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] -#[ test ] -fn vec_iters() -{ - - struct MyContainer - { - entries : Vec< i32 >, - } - - impl IntoIterator for MyContainer - { - type Item = i32; - type IntoIter = std::vec::IntoIter< i32 >; - // type IntoIter = the_module::vec::IntoIter< i32 >; - // qqq : should work - - fn into_iter( self ) -> Self::IntoIter - { - self.entries.into_iter() // Create an iterator from the internal HashSet. - } - } - - impl< 'a > IntoIterator for &'a MyContainer - { - type Item = &'a i32; - type IntoIter = std::slice::Iter< 'a, i32 >; - // type IntoIter = the_module::vec::Iter< 'a, i32 >; - // qqq : should work - - fn into_iter( self ) -> Self::IntoIter - { - self.entries.iter() // Borrow the elements via an iterator. - } - } - - let instance = MyContainer { entries : vec![ 1, 2, 3 ] }; - let got : Vec< _ > = ( &instance ).into_iter().cloned().collect(); - let exp = vec![ 1, 2, 3 ]; - a_id!( got, exp ); - - let instance = MyContainer { entries : vec![ 1, 2, 3 ] }; - let got : Vec< _ > = instance.into_iter().collect(); - let exp = vec![ 1, 2, 3 ]; - a_id!( got, exp ); - -} - // qqq : implement VectorInterface diff --git a/module/core/collection_tools/tests/inc/heap.rs b/module/core/collection_tools/tests/inc/heap.rs index 7da1ff7265..0ea57b20a7 100644 --- a/module/core/collection_tools/tests/inc/heap.rs +++ b/module/core/collection_tools/tests/inc/heap.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { diff --git a/module/core/collection_tools/tests/inc/hmap.rs b/module/core/collection_tools/tests/inc/hmap.rs index 0e6167ce1f..e64b992354 100644 --- a/module/core/collection_tools/tests/inc/hmap.rs +++ b/module/core/collection_tools/tests/inc/hmap.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { @@ -11,13 +11,13 @@ fn reexport() let got = *map1.get( &1 ).unwrap(); assert_eq!( exp, got ); -// let mut map2 : the_module::Map< i32, i32 > = the_module::Map::new(); -// map2.insert( 1, 2 ); -// let exp = 2; -// let got = *map2.get( &1 ).unwrap(); -// assert_eq!( exp, got ); + let mut map2 : the_module::Map< i32, i32 > = the_module::Map::new(); + map2.insert( 1, 2 ); + let exp = 2; + let got = *map2.get( &1 ).unwrap(); + assert_eq!( exp, got ); -// assert_eq!( map1, map2 ); + assert_eq!( map1, map2 ); } diff --git a/module/core/collection_tools/tests/inc/hset.rs b/module/core/collection_tools/tests/inc/hset.rs index 9241ef29d0..08fa4d5f9b 100644 --- a/module/core/collection_tools/tests/inc/hset.rs +++ b/module/core/collection_tools/tests/inc/hset.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { @@ -10,12 +10,12 @@ fn reexport() assert_eq!( set1.contains( &1 ), true ); assert_eq!( set1.contains( &2 ), false ); -// let mut set2 : the_module::Set< i32 > = the_module::Set::new(); -// set2.insert( 1 ); -// assert_eq!( set2.contains( &1 ), true ); -// assert_eq!( set2.contains( &2 ), false ); + let mut set2 : the_module::Set< i32 > = the_module::Set::new(); + set2.insert( 1 ); + assert_eq!( set2.contains( &1 ), true ); + assert_eq!( set2.contains( &2 ), false ); -// assert_eq!( set1, set2 ); + assert_eq!( set1, set2 ); } diff --git a/module/core/collection_tools/tests/inc/list.rs b/module/core/collection_tools/tests/inc/list.rs index 956d1af847..1637d8dc79 100644 --- a/module/core/collection_tools/tests/inc/list.rs +++ b/module/core/collection_tools/tests/inc/list.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { diff --git a/module/core/collection_tools/tests/inc/vec.rs b/module/core/collection_tools/tests/inc/vec.rs index 59d1896925..51419703a6 100644 --- a/module/core/collection_tools/tests/inc/vec.rs +++ b/module/core/collection_tools/tests/inc/vec.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { @@ -13,15 +13,15 @@ fn reexport() let got = vec1.last().unwrap().clone(); assert_eq!( got, 2 ); -// let mut vec2 : the_module::DynArray< i32 > = the_module::DynArray::new(); -// vec2.push( 1 ); -// vec2.push( 2 ); -// let got = vec2.first().unwrap().clone(); -// assert_eq!( got, 1 ); -// let got = vec2.last().unwrap().clone(); -// assert_eq!( got, 2 ); + let mut vec2 : the_module::DynArray< i32 > = the_module::DynArray::new(); + vec2.push( 1 ); + vec2.push( 2 ); + let got = vec2.first().unwrap().clone(); + assert_eq!( got, 1 ); + let got = vec2.last().unwrap().clone(); + assert_eq!( got, 2 ); -// assert_eq!( vec1, vec2 ); + assert_eq!( vec1, vec2 ); } @@ -62,3 +62,64 @@ fn into_constructor() assert_eq!( got, exp ); } + +// qqq : implement similar test for all containers +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn vec_iters() +{ + + struct MyContainer + { + entries : Vec< i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = i32; + type IntoIter = the_module::vec::IntoIter< i32 >; + // qqq : should work -- works + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() // Create an iterator from the internal HashSet. + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = &'a i32; + type IntoIter = std::slice::Iter< 'a, i32 >; + // type IntoIter = the_module::vec::Iter< 'a, i32 >; + // qqq : should work + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() // Borrow the elements via an iterator. + } + } + + impl< 'a > IntoIterator for &'a mut MyContainer + { + type Item = &'a mut i32; + type IntoIter = std::slice::IterMut< 'a, i32 >; + // type IntoIter = the_module::vec::IterMut< 'a, i32 >; + // qqq : should work + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter_mut() + } + } + + let instance = MyContainer { entries : vec![ 1, 2, 3 ] }; + let got : Vec< _ > = ( &instance ).into_iter().cloned().collect(); + let exp = vec![ 1, 2, 3 ]; + a_id!( got, exp ); + + let instance = MyContainer { entries : vec![ 1, 2, 3 ] }; + let got : Vec< _ > = instance.into_iter().collect(); + let exp = vec![ 1, 2, 3 ]; + a_id!( got, exp ); + +} \ No newline at end of file diff --git a/module/core/collection_tools/tests/inc/vecd.rs b/module/core/collection_tools/tests/inc/vecd.rs index b64073f993..5f44a29723 100644 --- a/module/core/collection_tools/tests/inc/vecd.rs +++ b/module/core/collection_tools/tests/inc/vecd.rs @@ -1,6 +1,6 @@ use super::*; -#[ cfg( not( feature = "no_std" ) ) ] +#[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ test ] fn reexport() { diff --git a/module/core/data_type/Cargo.toml b/module/core/data_type/Cargo.toml index d1d7ed2199..8b21b92043 100644 --- a/module/core/data_type/Cargo.toml +++ b/module/core/data_type/Cargo.toml @@ -54,7 +54,7 @@ no_std = [] use_alloc = [ "no_std" ] enabled = [] -dt_prelude = [ "collection_tools/prelude" ] +dt_prelude = [ "collection_tools/reexports" ] dt_interval = [ "interval_adapter/enabled" ] dt_collections = [ "collection_tools/enabled" ] dt_either = [ "either" ] From 94ad256772e8a92e776e24198ae90bf6ce183042 Mon Sep 17 00:00:00 2001 From: Anton Parfonov Date: Wed, 15 May 2024 14:43:55 +0300 Subject: [PATCH 2/2] Add iters tests --- .../collection_tools/src/collections/bmap.rs | 8 ++- .../collection_tools/src/collections/bset.rs | 8 ++- .../collection_tools/src/collections/heap.rs | 8 ++- .../collection_tools/src/collections/hmap.rs | 13 +++- .../collection_tools/src/collections/hset.rs | 13 +++- .../collection_tools/src/collections/list.rs | 8 ++- .../collection_tools/src/collections/vec.rs | 11 +++- .../collection_tools/src/collections/vecd.rs | 8 ++- module/core/collection_tools/src/lib.rs | 40 ++++++------- .../core/collection_tools/tests/inc/bmap.rs | 44 ++++++++++++++ .../core/collection_tools/tests/inc/bset.rs | 44 ++++++++++++++ .../core/collection_tools/tests/inc/heap.rs | 44 ++++++++++++++ .../core/collection_tools/tests/inc/hmap.rs | 60 +++++++++++++++++++ .../core/collection_tools/tests/inc/hset.rs | 44 ++++++++++++++ .../core/collection_tools/tests/inc/list.rs | 60 +++++++++++++++++++ module/core/collection_tools/tests/inc/vec.rs | 35 +++++------ .../core/collection_tools/tests/inc/vecd.rs | 59 ++++++++++++++++++ module/core/collection_tools/tests/tests.rs | 1 + 18 files changed, 452 insertions(+), 56 deletions(-) diff --git a/module/core/collection_tools/src/collections/bmap.rs b/module/core/collection_tools/src/collections/bmap.rs index 513e5f2753..e96f045e84 100644 --- a/module/core/collection_tools/src/collections/bmap.rs +++ b/module/core/collection_tools/src/collections/bmap.rs @@ -1,3 +1,7 @@ +#[ doc( inline ) ] +#[ allow( unused_imports ) ] +pub use alloc::collections::btree_map::*; + /// Creates a `BTreeMap` from a list of key-value pairs. /// /// The `bmap` macro facilitates the convenient creation of a `BTreeMap` with initial elements. @@ -66,7 +70,7 @@ macro_rules! bmap ) => {{ - let mut _map = collection_tools::BTreeMap::new(); + let mut _map = $crate::bmap::BTreeMap::new(); $( let _ = _map.insert( $key , $value ); )* @@ -159,7 +163,7 @@ macro_rules! into_bmap ) => {{ - let mut _map = collection_tools::BTreeMap::new(); + let mut _map = $crate::bmap::BTreeMap::new(); $( let _ = _map.insert( Into::into( $key ), Into::into( $value ) ); )* diff --git a/module/core/collection_tools/src/collections/bset.rs b/module/core/collection_tools/src/collections/bset.rs index 6b5dfc4226..c0c6d249ed 100644 --- a/module/core/collection_tools/src/collections/bset.rs +++ b/module/core/collection_tools/src/collections/bset.rs @@ -1,3 +1,7 @@ +#[ doc( inline ) ] +#[ allow( unused_imports ) ] +pub use alloc::collections::btree_set::*; + /// Creates a `BTreeSet` from a list of elements. /// /// The `bset` macro allows for convenient creation of a `BTreeSet` with initial elements. @@ -52,7 +56,7 @@ macro_rules! bset ) => {{ - let mut _set = collection_tools::BTreeSet::new(); + let mut _set = $crate::bset::BTreeSet::new(); $( _set.insert( $key ); )* @@ -145,7 +149,7 @@ macro_rules! into_bset ) => {{ - let mut _set = collection_tools::BTreeSet::new(); + let mut _set = $crate::bset::BTreeSet::new(); $( _set.insert( Into::into( $key ) ); )* diff --git a/module/core/collection_tools/src/collections/heap.rs b/module/core/collection_tools/src/collections/heap.rs index f76a302933..8d38492497 100644 --- a/module/core/collection_tools/src/collections/heap.rs +++ b/module/core/collection_tools/src/collections/heap.rs @@ -1,3 +1,7 @@ +#[ doc( inline ) ] +#[ allow( unused_imports ) ] + pub use alloc::collections::binary_heap::*; + /// Creates a `BinaryHeap` from a list of elements. /// /// The `into_heap` macro simplifies the creation of a `BinaryHeap` with initial elements. @@ -53,7 +57,7 @@ macro_rules! heap => {{ let _cap = count!( @count $( $key ),* ); - let mut _heap = collection_tools::BinaryHeap::with_capacity( _cap ); + let mut _heap = $crate::heap::BinaryHeap::with_capacity( _cap ); $( _heap.push( $key ); )* @@ -142,7 +146,7 @@ macro_rules! into_heap => {{ let _cap = count!( @count $( $key ),* ); - let mut _heap = collection_tools::BinaryHeap::with_capacity( _cap ); + let mut _heap = $crate::heap::BinaryHeap::with_capacity( _cap ); $( _heap.push( Into::into( $key ) ); )* diff --git a/module/core/collection_tools/src/collections/hmap.rs b/module/core/collection_tools/src/collections/hmap.rs index 47b7014aac..eceac4ee9b 100644 --- a/module/core/collection_tools/src/collections/hmap.rs +++ b/module/core/collection_tools/src/collections/hmap.rs @@ -1,3 +1,12 @@ +#[ cfg( 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 ) ] +pub use std::collections::hash_map::*; + /// Creates a `HashMap` from a list of key-value pairs. /// /// The `hmap` macro allows for convenient creation of a `HashMap` with initial elements. @@ -68,7 +77,7 @@ macro_rules! hmap => {{ let _cap = count!( @count $( $key ),* ); - let mut _map = collection_tools::HashMap::with_capacity( _cap ); + let mut _map = $crate::hmap::HashMap::with_capacity( _cap ); $( let _ = _map.insert( $key, $value ); )* @@ -163,7 +172,7 @@ macro_rules! into_hmap => {{ let _cap = count!( @count $( $key ),* ); - let mut _map = collection_tools::HashMap::with_capacity( _cap ); + let mut _map = $crate::hmap::HashMap::with_capacity( _cap ); $( let _ = _map.insert( Into::into( $key ), Into::into( $value ) ); )* diff --git a/module/core/collection_tools/src/collections/hset.rs b/module/core/collection_tools/src/collections/hset.rs index b23c535010..b9b2d682da 100644 --- a/module/core/collection_tools/src/collections/hset.rs +++ b/module/core/collection_tools/src/collections/hset.rs @@ -1,3 +1,12 @@ +#[ 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 ) ] +pub use std::collections::hash_set::*; + /// Creates a `HashSet` from a list of elements. /// /// The `hset` macro allows for convenient creation of a `HashSet` with initial elements. @@ -68,7 +77,7 @@ macro_rules! hset => {{ let _cap = count!( @count $( $key ),* ); - let mut _set = collection_tools::HashSet::with_capacity( _cap ); + let mut _set = $crate::hset::HashSet::with_capacity( _cap ); $( let _ = _set.insert( $key ); )* @@ -164,7 +173,7 @@ macro_rules! into_hset => {{ let _cap = count!( @count $( $key ),* ); - let mut _set = collection_tools::HashSet::with_capacity( _cap ); + let mut _set = $crate::hset::HashSet::with_capacity( _cap ); $( let _ = _set.insert( Into::into( $key ) ); )* diff --git a/module/core/collection_tools/src/collections/list.rs b/module/core/collection_tools/src/collections/list.rs index e01c4a4217..d7088ea77f 100644 --- a/module/core/collection_tools/src/collections/list.rs +++ b/module/core/collection_tools/src/collections/list.rs @@ -1,3 +1,7 @@ +#[ doc( inline ) ] +#[ allow( unused_imports ) ] +pub use alloc::collections::linked_list::*; + /// Creates a `LinkedList` from a list of elements. /// /// The `list` macro facilitates the creation of a `LinkedList` with initial elements. @@ -66,7 +70,7 @@ macro_rules! list {{ // "The LinkedList allows pushing and popping elements at either end in constant time." // So no `with_capacity` - let mut _lst = collection_tools::LinkedList::new(); + let mut _lst = $crate::list::LinkedList::new(); $( _lst.push_back( $key ); )* @@ -160,7 +164,7 @@ macro_rules! into_list {{ // "The LinkedList allows pushing and popping elements at either end in constant time." // So no `with_capacity` - let mut _lst = collection_tools::LinkedList::new(); + let mut _lst = $crate::list::LinkedList::new(); $( _lst.push_back( Into::into( $key ) ); )* diff --git a/module/core/collection_tools/src/collections/vec.rs b/module/core/collection_tools/src/collections/vec.rs index d9a5a4521c..2c19db388f 100644 --- a/module/core/collection_tools/src/collections/vec.rs +++ b/module/core/collection_tools/src/collections/vec.rs @@ -1,3 +1,10 @@ +#[ doc( inline ) ] +#[ allow( unused_imports ) ] +pub use alloc::vec::*; +#[ doc( inline ) ] +#[ allow( unused_imports ) ] +pub use core::slice::{ Iter, IterMut }; + /// Creates a `Vec` from a list of elements. /// /// The `vec` macro simplifies the creation of a `Vec` with initial elements. @@ -66,7 +73,7 @@ macro_rules! vec => {{ let _cap = count!( @count $( $key ),* ); - let mut _vec = collection_tools::Vec::with_capacity( _cap ); + let mut _vec = $crate::vec::Vec::with_capacity( _cap ); $( _vec.push( $key ); )* @@ -160,7 +167,7 @@ macro_rules! into_vec => {{ let _cap = count!( @count $( $key ),* ); - let mut _vec = collection_tools::Vec::with_capacity( _cap ); + let mut _vec = $crate::vec::Vec::with_capacity( _cap ); $( _vec.push( Into::into( $key ) ); )* diff --git a/module/core/collection_tools/src/collections/vecd.rs b/module/core/collection_tools/src/collections/vecd.rs index eb9369ea6f..2edff8b433 100644 --- a/module/core/collection_tools/src/collections/vecd.rs +++ b/module/core/collection_tools/src/collections/vecd.rs @@ -1,3 +1,7 @@ +#[ doc( inline ) ] +#[ allow( unused_imports ) ] +pub use alloc::collections::vec_deque::*; + /// Creates a `VecDeque` from a list of elements. /// /// The `vecd` macro allows for the convenient creation of a `VecDeque` with initial elements. @@ -71,7 +75,7 @@ macro_rules! vecd => {{ let _cap = count!( @count $( $key ),* ); - let mut _vecd = collection_tools::VecDeque::with_capacity( _cap ); + let mut _vecd = $crate::vecd::VecDeque::with_capacity( _cap ); $( _vecd.push_back( $key ); )* @@ -164,7 +168,7 @@ macro_rules! into_vecd => {{ let _cap = count!( @count $( $key ),* ); - let mut _vecd = collection_tools::VecDeque::with_capacity( _cap ); + let mut _vecd = $crate::vecd::VecDeque::with_capacity( _cap ); $( _vecd.push_back( Into::into( $key ) ); )* diff --git a/module/core/collection_tools/src/lib.rs b/module/core/collection_tools/src/lib.rs index c2faa8d516..93f360a73e 100644 --- a/module/core/collection_tools/src/lib.rs +++ b/module/core/collection_tools/src/lib.rs @@ -4,13 +4,16 @@ #![ doc( html_root_url = "https://docs.rs/collection_tools/latest/collection_tools/" ) ] #![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "Readme.md" ) ) ] +extern crate alloc; + // qqq : make subdirectory for each container -- done // qqq : move out of lib.rs file -- moved to `collections.rs` /// Module containing all collection macros #[ cfg( feature = "enabled" ) ] -pub mod collections; +mod collections; +pub use collections::*; /// Namespace with dependencies. #[ cfg( feature = "enabled" ) ] @@ -62,21 +65,21 @@ pub mod prelude { // qqq : for Anton : uncomment, make it working and cover by tests -- renamed to reexports - extern crate alloc; - - pub use alloc::collections; - - pub use alloc::vec; - - #[ cfg( feature = "use_alloc" ) ] - #[ doc( inline ) ] - #[ allow( unused_imports ) ] - pub use super::dependency::hashbrown as hash; - - #[ cfg( not( feature = "no_std" ) ) ] + #[ cfg( feature = "reexports" ) ] + #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] #[ doc( inline ) ] #[ allow( unused_imports ) ] - pub use std::collections as hash; + pub use crate:: + { + bmap::BTreeMap, + bset::BTreeSet, + heap::BinaryHeap, + hmap::HashMap, + hset::HashSet, + list::LinkedList, + vec::Vec, + vecd::VecDeque, + }; #[ cfg( feature = "reexports" ) ] #[ cfg( any( feature = "use_alloc", not( feature = "no_std" ) ) ) ] @@ -84,15 +87,6 @@ pub mod prelude #[ allow( unused_imports ) ] pub use { - collections::BTreeMap, - collections::BTreeSet, - collections::BinaryHeap, - hash::HashMap, - hash::HashSet, - collections::LinkedList, - vec::Vec, - collections::VecDeque, - HashMap as Map, HashSet as Set, Vec as DynArray, diff --git a/module/core/collection_tools/tests/inc/bmap.rs b/module/core/collection_tools/tests/inc/bmap.rs index 2fc68dd46b..d08e871a01 100644 --- a/module/core/collection_tools/tests/inc/bmap.rs +++ b/module/core/collection_tools/tests/inc/bmap.rs @@ -50,3 +50,47 @@ fn into_constructor() assert_eq!( got, exp ); } + +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn iters() +{ + + struct MyContainer + { + entries : the_module::BTreeMap< i32, i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = ( i32, i32 ); + type IntoIter = the_module::bmap::IntoIter< i32, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = ( &'a i32, &'a i32 ); + type IntoIter = the_module::bmap::Iter< 'a, i32, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() + } + } + + let instance = MyContainer { entries : the_module::BTreeMap::from( [ ( 1, 3 ), ( 2, 2 ), ( 3, 1 ) ] ) }; + let got : the_module::BTreeMap< _, _ > = instance.into_iter().collect(); + let exp = the_module::BTreeMap::from( [ ( 1, 3 ), ( 2, 2 ), ( 3, 1 ) ] ); + a_id!( got, exp ); + + let instance = MyContainer { entries : the_module::BTreeMap::from( [ ( 1, 3 ), ( 2, 2 ), ( 3, 1 ) ] ) }; + let got : the_module::BTreeMap< _, _ > = ( &instance ).into_iter().map( | ( k, v ) | ( k.clone(), v.clone() ) ).collect(); + let exp = the_module::BTreeMap::from( [ ( 1, 3 ), ( 2, 2 ), ( 3, 1 ) ] ); + a_id!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/bset.rs b/module/core/collection_tools/tests/inc/bset.rs index 00acb093bd..11a5c49a58 100644 --- a/module/core/collection_tools/tests/inc/bset.rs +++ b/module/core/collection_tools/tests/inc/bset.rs @@ -49,3 +49,47 @@ fn into_constructor() assert_eq!( got, exp ); } + +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn iters() +{ + + struct MyContainer + { + entries : the_module::BTreeSet< i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = i32; + type IntoIter = the_module::bset::IntoIter< i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = &'a i32; + type IntoIter = the_module::bset::Iter< 'a, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() + } + } + + let instance = MyContainer { entries : the_module::BTreeSet::from( [ 1, 2, 3 ] ) }; + let got : the_module::BTreeSet< _ > = instance.into_iter().collect(); + let exp = the_module::BTreeSet::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + + let instance = MyContainer { entries : the_module::BTreeSet::from( [ 1, 2, 3 ] ) }; + let got : the_module::BTreeSet< _ > = ( &instance ).into_iter().cloned().collect(); + let exp = the_module::BTreeSet::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/heap.rs b/module/core/collection_tools/tests/inc/heap.rs index 0ea57b20a7..632899d65f 100644 --- a/module/core/collection_tools/tests/inc/heap.rs +++ b/module/core/collection_tools/tests/inc/heap.rs @@ -50,3 +50,47 @@ fn into_constructor() assert_eq!( got.into_sorted_vec(), exp.into_sorted_vec() ); } + +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn iters() +{ + + struct MyContainer + { + entries : the_module::BinaryHeap< i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = i32; + type IntoIter = the_module::heap::IntoIter< i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = &'a i32; + type IntoIter = the_module::heap::Iter< 'a, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() + } + } + + let instance = MyContainer { entries : the_module::BinaryHeap::from( [ 1, 2, 3 ] ) }; + let got : the_module::BinaryHeap< i32 > = instance.into_iter().collect(); + let exp : the_module::BinaryHeap< i32 > = the_module::BinaryHeap::from( [ 1, 2, 3 ] ); + a_id!( got.into_sorted_vec(), exp.into_sorted_vec() ); + + let instance = MyContainer { entries : the_module::BinaryHeap::from( [ 1, 2, 3 ] ) }; + let got : the_module::BinaryHeap< i32 > = ( &instance ).into_iter().cloned().collect(); + let exp : the_module::BinaryHeap< i32 > = the_module::BinaryHeap::from( [ 1, 2, 3 ] ); + a_id!( got.into_sorted_vec(), exp.into_sorted_vec() ); + +} diff --git a/module/core/collection_tools/tests/inc/hmap.rs b/module/core/collection_tools/tests/inc/hmap.rs index e64b992354..04c8223e24 100644 --- a/module/core/collection_tools/tests/inc/hmap.rs +++ b/module/core/collection_tools/tests/inc/hmap.rs @@ -60,3 +60,63 @@ fn into_constructor() assert_eq!( got, exp ); } + +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn iters() +{ + + struct MyContainer + { + entries : the_module::HashMap< i32, i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = ( i32, i32 ); + type IntoIter = the_module::hmap::IntoIter< i32, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = ( &'a i32, &'a i32 ); + type IntoIter = the_module::hmap::Iter< 'a, i32, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() + } + } + + impl< 'a > IntoIterator for &'a mut MyContainer + { + type Item = ( &'a i32, &'a mut i32 ); + type IntoIter = the_module::hmap::IterMut< 'a, i32, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter_mut() + } + } + + let instance = MyContainer { entries : the_module::HashMap::from( [ ( 1 , 3 ), ( 2, 2 ), ( 3, 1 ) ] ) }; + let got : the_module::HashMap< _, _ > = instance.into_iter().collect(); + let exp = the_module::HashMap::from( [ ( 1 , 3 ), ( 2, 2 ), ( 3, 1 ) ] ); + a_id!( got, exp ); + + let instance = MyContainer { entries : the_module::HashMap::from( [ ( 1 , 3 ), ( 2, 2 ), ( 3, 1 ) ] ) }; + let got : the_module::HashMap< _, _ > = ( &instance ).into_iter().map( | ( k, v ) | ( k.clone(), v.clone() ) ).collect(); + let exp = the_module::HashMap::from( [ ( 1 , 3 ), ( 2, 2 ), ( 3, 1 ) ] ); + a_id!( got, exp ); + + let mut instance = MyContainer { entries : the_module::HashMap::from( [ ( 1 , 3 ), ( 2, 2 ), ( 3, 1 ) ] ) }; + ( &mut instance ).into_iter().for_each( | ( _, v ) | *v *= 2 ); + let exp = the_module::HashMap::from( [ ( 1, 6 ), ( 2 ,4 ), ( 3, 2 ) ] ); + a_id!( instance.entries, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/hset.rs b/module/core/collection_tools/tests/inc/hset.rs index 08fa4d5f9b..8b8ec3d2b4 100644 --- a/module/core/collection_tools/tests/inc/hset.rs +++ b/module/core/collection_tools/tests/inc/hset.rs @@ -56,3 +56,47 @@ fn into_constructor() assert_eq!( got, exp ); } + +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn iters() +{ + + struct MyContainer + { + entries : the_module::HashSet< i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = i32; + type IntoIter = the_module::hset::IntoIter< i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = &'a i32; + type IntoIter = the_module::hset::Iter< 'a, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() + } + } + + let instance = MyContainer { entries : the_module::HashSet::from( [ 1, 2, 3 ] ) }; + let got : the_module::HashSet< _ > = instance.into_iter().collect(); + let exp = the_module::HashSet::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + + let instance = MyContainer { entries : the_module::HashSet::from( [ 1, 2, 3 ] ) }; + let got : the_module::HashSet< _ > = ( &instance ).into_iter().cloned().collect(); + let exp = the_module::HashSet::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/list.rs b/module/core/collection_tools/tests/inc/list.rs index 1637d8dc79..b89daaf6f3 100644 --- a/module/core/collection_tools/tests/inc/list.rs +++ b/module/core/collection_tools/tests/inc/list.rs @@ -49,3 +49,63 @@ fn into_constructor() assert_eq!( got, exp ); } + +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn iters() +{ + + struct MyContainer + { + entries : the_module::LinkedList< i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = i32; + type IntoIter = the_module::list::IntoIter< i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = &'a i32; + type IntoIter = the_module::list::Iter< 'a, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() + } + } + + impl< 'a > IntoIterator for &'a mut MyContainer + { + type Item = &'a mut i32; + type IntoIter = the_module::list::IterMut< 'a, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter_mut() + } + } + + let instance = MyContainer { entries : the_module::LinkedList::from( [ 1, 2, 3 ] ) }; + let got : the_module::LinkedList< _ > = instance.into_iter().collect(); + let exp = the_module::LinkedList::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + + let instance = MyContainer { entries : the_module::LinkedList::from( [ 1, 2, 3 ] ) }; + let got : the_module::LinkedList< _ > = ( &instance ).into_iter().cloned().collect(); + let exp = the_module::LinkedList::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + + let mut instance = MyContainer { entries : the_module::LinkedList::from( [ 1, 2, 3 ] ) }; + ( &mut instance ).into_iter().for_each( | v | *v *= 2 ); + let exp = the_module::LinkedList::from( [ 2, 4, 6 ] ); + a_id!( instance.entries, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/vec.rs b/module/core/collection_tools/tests/inc/vec.rs index 51419703a6..d93062e9be 100644 --- a/module/core/collection_tools/tests/inc/vec.rs +++ b/module/core/collection_tools/tests/inc/vec.rs @@ -63,10 +63,10 @@ fn into_constructor() } -// qqq : implement similar test for all containers +// qqq : implement similar test for all containers -- done #[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] #[ test ] -fn vec_iters() +fn iters() { struct MyContainer @@ -82,29 +82,25 @@ fn vec_iters() fn into_iter( self ) -> Self::IntoIter { - self.entries.into_iter() // Create an iterator from the internal HashSet. + self.entries.into_iter() } } impl< 'a > IntoIterator for &'a MyContainer { type Item = &'a i32; - type IntoIter = std::slice::Iter< 'a, i32 >; - // type IntoIter = the_module::vec::Iter< 'a, i32 >; - // qqq : should work + type IntoIter = the_module::vec::Iter< 'a, i32 >; fn into_iter( self ) -> Self::IntoIter { - self.entries.iter() // Borrow the elements via an iterator. + self.entries.iter() } } impl< 'a > IntoIterator for &'a mut MyContainer { type Item = &'a mut i32; - type IntoIter = std::slice::IterMut< 'a, i32 >; - // type IntoIter = the_module::vec::IterMut< 'a, i32 >; - // qqq : should work + type IntoIter = the_module::vec::IterMut< 'a, i32 >; fn into_iter( self ) -> Self::IntoIter { @@ -112,14 +108,19 @@ fn vec_iters() } } - let instance = MyContainer { entries : vec![ 1, 2, 3 ] }; - let got : Vec< _ > = ( &instance ).into_iter().cloned().collect(); - let exp = vec![ 1, 2, 3 ]; + let instance = MyContainer { entries : the_module::Vec::from( [ 1, 2, 3 ] ) }; + let got : Vec< _ > = instance.into_iter().collect(); + let exp = the_module::Vec::from( [ 1, 2, 3 ] ); a_id!( got, exp ); - let instance = MyContainer { entries : vec![ 1, 2, 3 ] }; - let got : Vec< _ > = instance.into_iter().collect(); - let exp = vec![ 1, 2, 3 ]; + let instance = MyContainer { entries : the_module::Vec::from( [ 1, 2, 3 ] ) }; + let got : Vec< _ > = ( &instance ).into_iter().cloned().collect(); + let exp = the_module::Vec::from( [ 1, 2, 3 ] ); a_id!( got, exp ); -} \ No newline at end of file + let mut instance = MyContainer { entries : the_module::Vec::from( [ 1, 2, 3 ] ) }; + ( &mut instance ).into_iter().for_each( | v | *v *= 2 ); + let exp = the_module::Vec::from( [ 2, 4, 6 ] ); + a_id!( instance.entries, exp ); + +} diff --git a/module/core/collection_tools/tests/inc/vecd.rs b/module/core/collection_tools/tests/inc/vecd.rs index 5f44a29723..7e3260fd8b 100644 --- a/module/core/collection_tools/tests/inc/vecd.rs +++ b/module/core/collection_tools/tests/inc/vecd.rs @@ -49,3 +49,62 @@ fn into_constructor() assert_eq!( got, exp ); } + +#[ cfg( any( not( feature = "no_std" ), feature = "use_alloc" ) ) ] +#[ test ] +fn iters() +{ + struct MyContainer + { + entries : the_module::VecDeque< i32 >, + } + + impl IntoIterator for MyContainer + { + type Item = i32; + type IntoIter = the_module::vecd::IntoIter< i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.into_iter() + } + } + + impl< 'a > IntoIterator for &'a MyContainer + { + type Item = &'a i32; + type IntoIter = the_module::vecd::Iter< 'a, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter() + } + } + + impl< 'a > IntoIterator for &'a mut MyContainer + { + type Item = &'a mut i32; + type IntoIter = the_module::vecd::IterMut< 'a, i32 >; + + fn into_iter( self ) -> Self::IntoIter + { + self.entries.iter_mut() + } + } + + let instance = MyContainer { entries : the_module::VecDeque::from( [ 1, 2, 3 ] ) }; + let got : the_module::VecDeque< _ > = instance.into_iter().collect(); + let exp = the_module::VecDeque::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + + let instance = MyContainer { entries : the_module::VecDeque::from( [ 1, 2, 3 ] ) }; + let got : the_module::VecDeque< _ > = ( &instance ).into_iter().cloned().collect(); + let exp = the_module::VecDeque::from( [ 1, 2, 3 ] ); + a_id!( got, exp ); + + let mut instance = MyContainer { entries : the_module::VecDeque::from( [ 1, 2, 3 ] ) }; + ( &mut instance ).into_iter().for_each( | v | *v *= 2 ); + let exp = the_module::VecDeque::from( [ 2, 4, 6 ] ); + a_id!( instance.entries, exp ); + +} diff --git a/module/core/collection_tools/tests/tests.rs b/module/core/collection_tools/tests/tests.rs index b84a5dc030..eb256d2f63 100644 --- a/module/core/collection_tools/tests/tests.rs +++ b/module/core/collection_tools/tests/tests.rs @@ -9,4 +9,5 @@ use test_tools::exposed::*; #[ allow( unused_imports ) ] use ::collection_tools as the_module; +#[ cfg( feature = "enabled" ) ] mod inc;