rsmount 0.2.2

Safe Rust wrapper around the `util-linux/libmount` C library
Documentation
// Copyright (c) 2023 Nick Piaddo
// SPDX-License-Identifier: Apache-2.0 OR MIT

// From dependency library
use enum_iterator::all;

// From standard library
use std::marker::PhantomData;

// From this library
use crate::core::entries::UTabEntry;
use crate::owning_ref_from_ptr;
use crate::tables::Comparison;
use crate::tables::UTabDiff;

/// Differences between entries in two [`UTab`](crate::tables::UTab)s.
#[derive(Debug)]
pub struct UTabEntryDiff<'diff, 's, 'o> {
    source: Option<&'diff UTabEntry>,
    other: Option<&'diff UTabEntry>,
    comparisons: Vec<Comparison>,
    _marker: PhantomData<&'diff UTabDiff<'s, 'o>>,
}

impl<'diff, 's, 'o> UTabEntryDiff<'diff, 's, 'o> {
    #[doc(hidden)]
    /// Creates a new instance.
    #[allow(dead_code)]
    pub(crate) fn new(
        table_diff: &'diff UTabDiff<'s, 'o>,
        source_entry_inner: *mut libmount::libmnt_fs,
        other_entry_inner: *mut libmount::libmnt_fs,
        flags: i32,
    ) -> UTabEntryDiff<'diff, 's, 'o> {
        let source = if source_entry_inner.is_null() {
            None
        } else {
            let entry = owning_ref_from_ptr!(table_diff, UTabEntry, source_entry_inner);

            Some(entry)
        };

        let other = if other_entry_inner.is_null() {
            None
        } else {
            let entry = owning_ref_from_ptr!(table_diff, UTabEntry, other_entry_inner);

            Some(entry)
        };

        let comparisons: Vec<_> = all::<Comparison>()
            .filter(|&op| flags & (op as i32) != 0)
            .collect();

        Self {
            source,
            other,
            comparisons,
            _marker: PhantomData,
        }
    }

    /// Returns the entry used as the reference for the comparison.
    pub fn source(&self) -> Option<&'diff UTabEntry> {
        self.source
    }

    /// Returns the entry the reference is compared to.
    pub fn other(&self) -> Option<&'diff UTabEntry> {
        self.other
    }

    /// Returns a list of the [`Comparison`]s performed.
    pub fn comparisons(&self) -> &[Comparison] {
        &self.comparisons
    }
}