use crate::{block, Vote};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{ops::Deref, slice};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct LastCommit {
pub block_id: block::Id,
pub precommits: Precommits,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct Precommits(Option<Vec<Option<Vote>>>);
impl Precommits {
pub fn new<I>(into_precommits: I) -> Self
where
I: Into<Vec<Option<Vote>>>,
{
Self(Some(into_precommits.into()))
}
pub fn into_vec(self) -> Vec<Option<Vote>> {
self.iter().cloned().collect()
}
pub fn iter(&self) -> slice::Iter<Option<Vote>> {
self.as_ref().iter()
}
}
impl AsRef<[Option<Vote>]> for Precommits {
fn as_ref(&self) -> &[Option<Vote>] {
self.0.as_ref().map(Vec::as_slice).unwrap_or_else(|| &[])
}
}
impl Deref for Precommits {
type Target = [Option<Vote>];
fn deref(&self) -> &[Option<Vote>] {
self.as_ref()
}
}