-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1440 from Sakapoi/fields_impl
READY: added impl and tests
- Loading branch information
Showing
10 changed files
with
535 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! | ||
//! Implement fields for BTreeSet. | ||
//! | ||
use crate::*; | ||
use std::borrow::Cow; | ||
use collection_tools::BTreeSet; | ||
|
||
impl< V, Borrowed > Fields< usize, &'_ Borrowed > for BTreeSet< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = &'v Borrowed | ||
where Self : 'v, V : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, val.borrow() ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed > Fields< usize, Option< Cow< '_, Borrowed > > > for BTreeSet< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = Option< Cow< 'v, Borrowed > > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, Some( Cow::Borrowed( val.borrow() ) ) ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed, Marker > Fields< usize, OptionalCow< '_, Borrowed, Marker > > for BTreeSet< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
Marker : Clone + Copy + 'static, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = OptionalCow< 'v, Borrowed, Marker > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, OptionalCow::from( val.borrow() ) ) ) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! | ||
//! Implement fields for Deque. | ||
//! | ||
use crate::*; | ||
use std::borrow::Cow; | ||
use collection_tools::VecDeque; | ||
|
||
impl< V, Borrowed > Fields< usize, &'_ Borrowed > for VecDeque< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = &'v Borrowed | ||
where Self : 'v, V : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, val.borrow() ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed > Fields< usize, Option< Cow< '_, Borrowed > > > for VecDeque< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = Option< Cow< 'v, Borrowed > > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, Some( Cow::Borrowed( val.borrow() ) ) ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed, Marker > Fields< usize, OptionalCow< '_, Borrowed, Marker > > for VecDeque< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
Marker : Clone + Copy + 'static, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = OptionalCow< 'v, Borrowed, Marker > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, OptionalCow::from( val.borrow() ) ) ) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! | ||
//! Implement fields for HashSet. | ||
//! | ||
use crate::*; | ||
use std::borrow::Cow; | ||
use std::collections::HashSet; | ||
|
||
impl< V, Borrowed > Fields< usize, &'_ Borrowed > for HashSet< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = &'v Borrowed | ||
where Self : 'v, V : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, val.borrow() ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed > Fields< usize, Option< Cow< '_, Borrowed > > > for HashSet< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = Option< Cow< 'v, Borrowed > > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, Some( Cow::Borrowed( val.borrow() ) ) ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed, Marker > Fields< usize, OptionalCow< '_, Borrowed, Marker > > for HashSet< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
Marker : Clone + Copy + 'static, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = OptionalCow< 'v, Borrowed, Marker > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, OptionalCow::from( val.borrow() ) ) ) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! | ||
//! Implement fields for LinkedList. | ||
//! | ||
use crate::*; | ||
use std::borrow::Cow; | ||
use collection_tools::LinkedList; | ||
|
||
impl< V, Borrowed > Fields< usize, &'_ Borrowed > for LinkedList< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = &'v Borrowed | ||
where Self : 'v, V : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, val.borrow() ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed > Fields< usize, Option< Cow< '_, Borrowed > > > for LinkedList< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = Option< Cow< 'v, Borrowed > > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, Some( Cow::Borrowed( val.borrow() ) ) ) ) | ||
} | ||
|
||
} | ||
|
||
impl< V, Borrowed, Marker > Fields< usize, OptionalCow< '_, Borrowed, Marker > > for LinkedList< V > | ||
where | ||
Borrowed : std::borrow::ToOwned + 'static + ?Sized, | ||
V : std::borrow::Borrow< Borrowed >, | ||
Marker : Clone + Copy + 'static, | ||
{ | ||
|
||
type Key< 'k > = usize | ||
where Self : 'k, usize : 'k; | ||
|
||
type Val< 'v > = OptionalCow< 'v, Borrowed, Marker > | ||
where Self : 'v; | ||
|
||
fn fields< 's >( &'s self ) -> impl IteratorTrait< Item = ( Self::Key< 's >, Self::Val< 's > ) > | ||
{ | ||
self.iter().enumerate().map( move | ( key, val ) | ( key, OptionalCow::from( val.borrow() ) ) ) | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
module/core/reflect_tools/tests/inc/fundamental/fields_bset.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#[ allow( unused_imports ) ] | ||
use super::*; | ||
|
||
use the_module:: | ||
{ | ||
Fields, | ||
|
||
}; | ||
|
||
// xxx : implement for other collections | ||
|
||
use std:: | ||
{ | ||
borrow::Cow, | ||
}; | ||
|
||
#[ test ] | ||
fn bset_string_fields() | ||
{ | ||
let collection : BTreeSet< String > = bset! | ||
[ | ||
"a".to_string(), | ||
"b".to_string(), | ||
]; | ||
|
||
// k, v | ||
let got : BTreeSet< _ > = Fields::< usize, &str >::fields( &collection ).collect(); | ||
assert_eq!( got.len(), 2 ); | ||
let exp = bset![ ( 0, "a" ), ( 1, "b" ) ]; | ||
assert_eq!( got, exp ); | ||
|
||
// k, Option< Cow< '_, str > > | ||
let got : BTreeSet< _ > = Fields::< usize, Option< Cow< '_, str > > >::fields( &collection ).collect(); | ||
assert_eq!( got.len(), 2 ); | ||
let exp = bset![ ( 0, Some( Cow::Borrowed( "a" ) ) ), ( 1, Some( Cow::Borrowed( "b" ) ) ) ]; | ||
assert_eq!( got, exp ); | ||
|
||
} | ||
|
||
#[ test ] | ||
fn bset_str_fields() | ||
{ | ||
let collection : BTreeSet< &str > = bset! | ||
[ | ||
"a", | ||
"b", | ||
]; | ||
|
||
// k, v | ||
let got : BTreeSet< _ > = Fields::< usize, &str >::fields( &collection ).collect(); | ||
assert_eq!( got.len(), 2 ); | ||
let exp = bset![ ( 0, "a" ), ( 1, "b" ) ]; | ||
assert_eq!( got, exp ); | ||
|
||
// k, Option< Cow< '_, str > > | ||
let got : BTreeSet< _ > = Fields::< usize, Option< Cow< '_, str > > >::fields( &collection ).collect(); | ||
assert_eq!( got.len(), 2 ); | ||
let exp = bset![ ( 0, Some( Cow::Borrowed( "a" ) ) ), ( 1, Some( Cow::Borrowed( "b" ) ) ) ]; | ||
assert_eq!( got, exp ); | ||
|
||
} |
Oops, something went wrong.