@ -29,7 +29,10 @@
//!
//!
//! The code above uses `HashMap`, but all the concrete map types implement the
//! The code above uses `HashMap`, but all the concrete map types implement the
//! `TryFrom` trait.
//! `TryFrom` trait.
use std ::{ convert ::TryFrom , ffi ::CString , io , mem , ops ::Deref , os ::unix ::io ::RawFd , ptr } ;
use std ::{
convert ::TryFrom , ffi ::CString , io , marker ::PhantomData , mem , ops ::Deref , os ::unix ::io ::RawFd ,
ptr ,
} ;
use thiserror ::Error ;
use thiserror ::Error ;
use crate ::{
use crate ::{
@ -148,19 +151,20 @@ impl Map {
}
}
pub ( crate ) trait IterableMap < K : Pod , V > {
pub ( crate ) trait IterableMap < K : Pod , V > {
fn fd ( & self ) -> Result < RawFd , MapError > ;
fn map ( & self ) -> & Map ;
unsafe fn get ( & self , key : & K ) -> Result < V , MapError > ;
unsafe fn get ( & self , key : & K ) -> Result < V , MapError > ;
}
}
/// Iterator returned by `map.keys()`.
/// Iterator returned by `map.keys()`.
pub struct MapKeys < ' coll , K : Pod , V > {
pub struct MapKeys < ' coll , K : Pod > {
map : & ' coll dyn Iterable Map< K , V > ,
map : & ' coll Map,
err : bool ,
err : bool ,
key : Option < K > ,
key : Option < K > ,
}
}
impl < ' coll , K : Pod , V > MapKeys < ' coll , K , V > {
impl < ' coll , K : Pod > MapKeys < ' coll , K > {
fn new ( map : & ' coll dyn Iterable Map< K , V > ) -> MapKeys < ' coll , K , V > {
fn new ( map : & ' coll Map) -> MapKeys < ' coll , K > {
MapKeys {
MapKeys {
map ,
map ,
err : false ,
err : false ,
@ -169,7 +173,7 @@ impl<'coll, K: Pod, V> MapKeys<'coll, K, V> {
}
}
}
}
impl < K : Pod , V > Iterator for MapKeys < ' _ , K , V > {
impl < K : Pod > Iterator for MapKeys < ' _ , K > {
type Item = Result < K , MapError > ;
type Item = Result < K , MapError > ;
fn next ( & mut self ) -> Option < Result < K , MapError > > {
fn next ( & mut self ) -> Option < Result < K , MapError > > {
@ -177,7 +181,7 @@ impl<K: Pod, V> Iterator for MapKeys<'_, K, V> {
return None ;
return None ;
}
}
let fd = match self . map . fd ( ) {
let fd = match self . map . fd _or_err ( ) {
Ok ( fd ) = > fd ,
Ok ( fd ) = > fd ,
Err ( e ) = > {
Err ( e ) = > {
self . err = true ;
self . err = true ;
@ -208,13 +212,17 @@ impl<K: Pod, V> Iterator for MapKeys<'_, K, V> {
/// Iterator returned by `map.iter()`.
/// Iterator returned by `map.iter()`.
pub struct MapIter < ' coll , K : Pod , V > {
pub struct MapIter < ' coll , K : Pod , V > {
inner : MapKeys < ' coll , K , V > ,
keys : MapKeys < ' coll , K > ,
map : & ' coll dyn IterableMap < K , V > ,
_v : PhantomData < V > ,
}
}
impl < ' coll , K : Pod , V > MapIter < ' coll , K , V > {
impl < ' coll , K : Pod , V > MapIter < ' coll , K , V > {
fn new ( map : & ' coll dyn IterableMap < K , V > ) -> MapIter < ' coll , K , V > {
fn new ( map : & ' coll dyn IterableMap < K , V > ) -> MapIter < ' coll , K , V > {
MapIter {
MapIter {
inner : MapKeys ::new ( map ) ,
keys : MapKeys ::new ( map . map ( ) ) ,
map ,
_v : PhantomData ,
}
}
}
}
}
}
@ -224,9 +232,9 @@ impl<K: Pod, V> Iterator for MapIter<'_, K, V> {
fn next ( & mut self ) -> Option < Self ::Item > {
fn next ( & mut self ) -> Option < Self ::Item > {
loop {
loop {
match self . inner . next ( ) {
match self . keys . next ( ) {
Some ( Ok ( key ) ) = > {
Some ( Ok ( key ) ) = > {
let value = unsafe { self . inner. map. get ( & key ) } ;
let value = unsafe { self . map. get ( & key ) } ;
match value {
match value {
Ok ( value ) = > return Some ( Ok ( ( key , value ) ) ) ,
Ok ( value ) = > return Some ( Ok ( ( key , value ) ) ) ,
Err ( MapError ::KeyNotFound ) = > continue ,
Err ( MapError ::KeyNotFound ) = > continue ,