use access::dict::BDictAccess;
use access::list::BListAccess;
pub enum BencodeRefKind<'a, K: 'a, V: 'a> {
Int(i64),
Bytes(&'a [u8]),
List(&'a BListAccess<V>),
Dict(&'a BDictAccess<K, V>),
}
pub trait BRefAccess: Sized {
type BKey;
type BType: BRefAccess<BKey=Self::BKey>;
fn kind<'a>(&'a self) -> BencodeRefKind<'a, Self::BKey, Self::BType>;
fn str(&self) -> Option<&str>;
fn int(&self) -> Option<i64>;
fn bytes(&self) -> Option<&[u8]>;
fn list(&self) -> Option<&BListAccess<Self::BType>>;
fn dict(&self) -> Option<&BDictAccess<Self::BKey, Self::BType>>;
}
pub trait BRefAccessExt<'a>: BRefAccess {
fn str_ext(&self) -> Option<&'a str>;
fn bytes_ext(&self) -> Option<&'a [u8]>;
}
impl<'a, T> BRefAccess for &'a T
where T: BRefAccess {
type BKey = T::BKey;
type BType = T::BType;
fn kind<'b>(&'b self) -> BencodeRefKind<'b, Self::BKey, Self::BType> {
(*self).kind()
}
fn str(&self) -> Option<&str> {
(*self).str()
}
fn int(&self) -> Option<i64> {
(*self).int()
}
fn bytes(&self) -> Option<&[u8]> {
(*self).bytes()
}
fn list(&self) -> Option<&BListAccess<Self::BType>> {
(*self).list()
}
fn dict(&self) -> Option<&BDictAccess<Self::BKey, Self::BType>> {
(*self).dict()
}
}
impl<'a: 'b, 'b, T> BRefAccessExt<'a> for &'b T
where T: BRefAccessExt<'a> {
fn str_ext(&self) -> Option<&'a str> {
(*self).str_ext()
}
fn bytes_ext(&self) -> Option<&'a [u8]> {
(*self).bytes_ext()
}
}
pub enum BencodeMutKind<'a, K: 'a, V: 'a> {
Int(i64),
Bytes(&'a [u8]),
List(&'a mut BListAccess<V>),
Dict(&'a mut BDictAccess<K, V>),
}
pub trait BMutAccess: Sized + BRefAccess {
fn kind_mut<'a>(&'a mut self) -> BencodeMutKind<'a, Self::BKey, Self::BType>;
fn list_mut(&mut self) -> Option<&mut BListAccess<Self::BType>>;
fn dict_mut(&mut self) -> Option<&mut BDictAccess<Self::BKey, Self::BType>>;
}