union store: introduce common type for the union store implmentation

Summary:
We will be implmenting multiple union stores and therefore, it makes
sense to encapsulate the common logic in its own type. This also abstracts the
usage of `RefCell` within the union store.

Reviewed By: jsgf

Differential Revision: D7884651

fbshipit-source-id: a74b6d9df5ee0d7d04359219e276fd5713b3a00b
This commit is contained in:
Saurabh Singh 2018-05-15 18:35:50 -07:00 committed by Facebook Github Bot
parent bcb7ac0b32
commit d2b9c6c6ac
2 changed files with 48 additions and 0 deletions

View File

@ -5,6 +5,8 @@
#[macro_use]
extern crate failure;
mod unionstore;
pub mod error;
pub mod datastore;
pub mod key;

View File

@ -0,0 +1,46 @@
// Copyright Facebook, Inc. 2018
// Union store
use std::cell::RefCell;
use std::vec::IntoIter;
pub struct UnionStore<T> {
stores: RefCell<Vec<T>>,
}
pub struct UnionStoreIterator<T>(IntoIter<T>);
impl<T> UnionStore<T> {
pub fn new() -> UnionStore<T> {
UnionStore {
stores: RefCell::new(vec![]),
}
}
pub fn add(&mut self, item: T)
where
T: Clone,
{
self.stores.borrow_mut().push(item)
}
}
impl<'a, T> IntoIterator for &'a UnionStore<T>
where
T: Clone,
{
type Item = T;
type IntoIter = UnionStoreIterator<T>;
fn into_iter(self) -> Self::IntoIter {
UnionStoreIterator(self.stores.borrow().clone().into_iter())
}
}
impl<T> Iterator for UnionStoreIterator<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}