use std::fmt;
use std::future::IntoFuture;
#[cfg(any(rings_native, rings_browser))]
use futures::future::join_all;
use futures::future::BoxFuture;
#[cfg(any(rings_native, rings_browser))]
use futures::FutureExt;
#[cfg(rings_native)]
use rings_core::storage::file::StoreRelease;
#[cfg(rings_browser)]
use crate::provider::browser::provider::lock::BrowserLockRelease;
#[derive(Clone, Default)]
pub struct StoresReleased {
#[cfg(rings_native)]
stores: Vec<StoreRelease>,
#[cfg(rings_browser)]
stores: Vec<BrowserLockRelease>,
}
impl StoresReleased {
#[cfg(rings_native)]
pub fn of(stores: impl IntoIterator<Item = StoreRelease>) -> Self {
Self {
stores: stores.into_iter().collect(),
}
}
#[cfg(rings_browser)]
pub fn of(stores: impl IntoIterator<Item = BrowserLockRelease>) -> Self {
Self {
stores: stores.into_iter().collect(),
}
}
pub fn len(&self) -> usize {
#[cfg(any(rings_native, rings_browser))]
{
self.stores.len()
}
#[cfg(not(any(rings_native, rings_browser)))]
{
0
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl IntoFuture for StoresReleased {
type Output = ();
type IntoFuture = BoxFuture<'static, ()>;
fn into_future(self) -> Self::IntoFuture {
#[cfg(any(rings_native, rings_browser))]
{
join_all(self.stores).map(drop).boxed()
}
#[cfg(not(any(rings_native, rings_browser)))]
{
Box::pin(std::future::ready(()))
}
}
}
impl fmt::Debug for StoresReleased {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StoresReleased")
.field("stores", &self.len())
.finish()
}
}