Much work

This commit is contained in:
Carl Lerche
2017-08-03 15:50:13 -07:00
parent 7a804601c5
commit dd8412d660
11 changed files with 211 additions and 104 deletions

View File

@@ -1,32 +1,32 @@
extern crate slab;
use super::*;
use slab;
use std::collections::{HashMap, hash_map};
/// Storage for streams
#[derive(Debug)]
pub struct Store {
slab: slab::Slab<State>,
pub(super) struct Store<B> {
slab: slab::Slab<Stream<B>>,
ids: HashMap<StreamId, usize>,
}
pub enum Entry<'a> {
Occupied(OccupiedEntry<'a>),
Vacant(VacantEntry<'a>),
pub(super) enum Entry<'a, B: 'a> {
Occupied(OccupiedEntry<'a, B>),
Vacant(VacantEntry<'a, B>),
}
pub struct OccupiedEntry<'a> {
pub(super) struct OccupiedEntry<'a, B: 'a> {
ids: hash_map::OccupiedEntry<'a, StreamId, usize>,
slab: &'a mut slab::Slab<State>,
slab: &'a mut slab::Slab<Stream<B>>,
}
pub struct VacantEntry<'a> {
pub(super) struct VacantEntry<'a, B: 'a> {
ids: hash_map::VacantEntry<'a, StreamId, usize>,
slab: &'a mut slab::Slab<State>,
slab: &'a mut slab::Slab<Stream<B>>,
}
impl Store {
impl<B> Store<B> {
pub fn new() -> Self {
Store {
slab: slab::Slab::new(),
@@ -34,7 +34,7 @@ impl Store {
}
}
pub fn get_mut(&mut self, id: &StreamId) -> Option<&mut State> {
pub fn get_mut(&mut self, id: &StreamId) -> Option<&mut Stream<B>> {
if let Some(handle) = self.ids.get(id) {
Some(&mut self.slab[*handle])
} else {
@@ -42,12 +42,12 @@ impl Store {
}
}
pub fn insert(&mut self, id: StreamId, val: State) {
pub fn insert(&mut self, id: StreamId, val: Stream<B>) {
let handle = self.slab.insert(val);
assert!(self.ids.insert(id, handle).is_none());
}
pub fn entry(&mut self, id: StreamId) -> Entry {
pub fn entry(&mut self, id: StreamId) -> Entry<B> {
use self::hash_map::Entry::*;
match self.ids.entry(id) {
@@ -67,14 +67,14 @@ impl Store {
}
}
impl<'a> OccupiedEntry<'a> {
pub fn into_mut(self) -> &'a mut State {
impl<'a, B> OccupiedEntry<'a, B> {
pub fn into_mut(self) -> &'a mut Stream<B> {
&mut self.slab[*self.ids.get()]
}
}
impl<'a> VacantEntry<'a> {
pub fn insert(self, value: State) -> &'a mut State {
impl<'a, B> VacantEntry<'a, B> {
pub fn insert(self, value: Stream<B>) -> &'a mut Stream<B> {
// Insert the value in the slab
let handle = self.slab.insert(value);