use crate::memory::VmAddr;
use alloc::boxed::Box;
use alloc::vec::Vec;
#[derive(Clone, Debug, Default)]
pub struct Lifecycle {
funcs: Vec<VmAddr>,
}
impl Lifecycle {
pub(crate) fn new(func: Option<VmAddr>, func_array: Option<Box<[VmAddr]>>) -> Self {
let len = usize::from(func.is_some()) + func_array.as_ref().map_or(0, |array| array.len());
let mut funcs = Vec::with_capacity(len);
funcs.extend(func);
if let Some(array) = func_array {
funcs.extend(array);
}
Self { funcs }
}
#[inline]
pub fn func_addrs(&self) -> impl Iterator<Item = VmAddr> + '_ {
self.funcs.iter().copied()
}
#[inline]
pub fn as_slice(&self) -> &[VmAddr] {
&self.funcs
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [VmAddr] {
&mut self.funcs
}
#[inline]
pub fn push(&mut self, addr: VmAddr) {
self.funcs.push(addr);
}
#[inline]
pub fn extend<I>(&mut self, addrs: I)
where
I: IntoIterator<Item = VmAddr>,
{
self.funcs.extend(addrs);
}
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(VmAddr) -> bool,
{
self.funcs.retain(|addr| f(*addr));
}
#[inline]
pub fn clear(&mut self) {
self.funcs.clear();
}
#[inline]
pub fn is_empty(&self) -> bool {
self.funcs.is_empty()
}
}