Skip to content

Commit

Permalink
Merge pull request #1440 from Sakapoi/fields_impl
Browse files Browse the repository at this point in the history
READY: added impl and tests
  • Loading branch information
Wandalen authored Aug 29, 2024
2 parents 871ea36 + 35f8b30 commit bb4b764
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 0 deletions.
4 changes: 4 additions & 0 deletions module/core/reflect_tools/src/reflect/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ mod private
mod vec;
mod hmap;
mod bmap;
mod hset;
mod bset;
mod deque;
mod llist;

#[ doc( inline ) ]
#[ allow( unused_imports ) ]
Expand Down
65 changes: 65 additions & 0 deletions module/core/reflect_tools/src/reflect/fields/bset.rs
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() ) ) )
}

}
65 changes: 65 additions & 0 deletions module/core/reflect_tools/src/reflect/fields/deque.rs
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() ) ) )
}

}
65 changes: 65 additions & 0 deletions module/core/reflect_tools/src/reflect/fields/hset.rs
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() ) ) )
}

}
65 changes: 65 additions & 0 deletions module/core/reflect_tools/src/reflect/fields/llist.rs
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 module/core/reflect_tools/tests/inc/fundamental/fields_bset.rs
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 );

}
Loading

0 comments on commit bb4b764

Please sign in to comment.