use enum_iterator::all;
use std::marker::PhantomData;
use crate::core::entries::SwapsEntry;
use crate::owning_ref_from_ptr;
use crate::tables::Comparison;
use crate::tables::SwapsDiff;
#[derive(Debug)]
pub struct SwapsEntryDiff<'diff, 's, 'o> {
source: Option<&'diff SwapsEntry>,
other: Option<&'diff SwapsEntry>,
comparisons: Vec<Comparison>,
_marker: PhantomData<&'diff SwapsDiff<'s, 'o>>,
}
impl<'diff, 's, 'o> SwapsEntryDiff<'diff, 's, 'o> {
#[doc(hidden)]
#[allow(dead_code)]
pub(crate) fn new(
table_diff: &'diff SwapsDiff<'s, 'o>,
source_entry_inner: *mut libmount::libmnt_fs,
other_entry_inner: *mut libmount::libmnt_fs,
flags: i32,
) -> SwapsEntryDiff<'diff, 's, 'o> {
let source = if source_entry_inner.is_null() {
None
} else {
let entry = owning_ref_from_ptr!(table_diff, SwapsEntry, source_entry_inner);
Some(entry)
};
let other = if other_entry_inner.is_null() {
None
} else {
let entry = owning_ref_from_ptr!(table_diff, SwapsEntry, other_entry_inner);
Some(entry)
};
let comparisons: Vec<_> = all::<Comparison>()
.filter(|&op| flags & (op as i32) != 0)
.collect();
Self {
source,
other,
comparisons,
_marker: PhantomData,
}
}
pub fn source(&self) -> Option<&'diff SwapsEntry> {
self.source
}
pub fn other(&self) -> Option<&'diff SwapsEntry> {
self.other
}
pub fn comparisons(&self) -> &[Comparison] {
&self.comparisons
}
}