mirror of
https://github.com/facebook/sapling.git
synced 2024-10-08 15:57:43 +03:00
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:
parent
bcb7ac0b32
commit
d2b9c6c6ac
@ -5,6 +5,8 @@
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
|
||||
mod unionstore;
|
||||
|
||||
pub mod error;
|
||||
pub mod datastore;
|
||||
pub mod key;
|
||||
|
46
lib/revisionstore/src/unionstore.rs
Normal file
46
lib/revisionstore/src/unionstore.rs
Normal 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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user