libparted/
alignment.rs

1use libparted_sys::{
2    ped_alignment_align_down, ped_alignment_align_nearest, ped_alignment_align_up,
3    ped_alignment_destroy, ped_alignment_duplicate, ped_alignment_init, ped_alignment_intersect,
4    ped_alignment_is_aligned, ped_alignment_new, PedAlignment,
5};
6
7use super::{cvt, get_optional, Geometry};
8use std::io;
9use std::marker::PhantomData;
10
11pub struct Alignment<'a> {
12    pub(crate) alignment: *mut PedAlignment,
13    pub(crate) phantom: PhantomData<&'a PedAlignment>,
14}
15
16impl<'a> Alignment<'a> {
17    pub fn from_raw(alignment: *mut PedAlignment) -> Alignment<'a> {
18        Alignment {
19            alignment,
20            phantom: PhantomData,
21        }
22    }
23
24    /// Return an alignment object representing all sectors that are of the form
25    /// `offset + X * grain_size`.
26    pub fn new(offset: i64, grain_size: i64) -> io::Result<Alignment<'a>> {
27        cvt(unsafe { ped_alignment_new(offset, grain_size) }).map(Alignment::from_raw)
28    }
29
30    /// Initializes a preallocated piece of memory for an alignment object.
31    ///
32    /// The object will represent all sectors for which the equation
33    /// `S = offset + x * grain_size` holds.
34    pub fn init(&mut self, offset: i64, grain_size: i64) -> io::Result<()> {
35        cvt(unsafe { ped_alignment_init(self.alignment, offset, grain_size) })?;
36        Ok(())
37    }
38
39    /// Returns the sector that is closest to `sector`, satifies the `align` constraint, and lies
40    /// lies inside `geom`.
41    pub fn align_down(&self, geom: &Geometry, sector: i64) -> Option<u64> {
42        match unsafe { ped_alignment_align_down(self.alignment, geom.geometry, sector) } {
43            -1 => None,
44            sector => Some(sector as u64),
45        }
46    }
47
48    /// Returns the sector that is closest to `sector`, satisfies the `align` constraint, and
49    /// lies inside of `geom`.
50    pub fn align_nearest(&self, geom: &Geometry, sector: i64) -> Option<u64> {
51        match unsafe { ped_alignment_align_nearest(self.alignment, geom.geometry, sector) } {
52            -1 => None,
53            sector => Some(sector as u64),
54        }
55    }
56
57    /// Returns the sector that is closest to `sector`, satifies the `align` constraint, and lies
58    /// lies inside `geom`.
59    pub fn align_up(&self, geom: &Geometry, sector: i64) -> Option<u64> {
60        match unsafe { ped_alignment_align_up(self.alignment, geom.geometry, sector) } {
61            -1 => None,
62            sector => Some(sector as u64),
63        }
64    }
65
66    /// Clones and returns a duplicate of the alignment, if possible.
67    pub fn duplicate<'b>(&self) -> io::Result<Alignment<'b>> {
68        cvt(unsafe { ped_alignment_duplicate(self.alignment) }).map(|alignment| Alignment {
69            alignment,
70            phantom: PhantomData,
71        })
72    }
73
74    pub fn grain_size(&self) -> i64 {
75        unsafe { (*self.alignment).grain_size }
76    }
77
78    /// Returns a new **Alignment** object if an intersection can between
79    /// itself and a given `other` **Alignment**.
80    pub fn intersect(&self, other: &Alignment) -> Option<Alignment<'a>> {
81        get_optional(unsafe { ped_alignment_intersect(self.alignment, other.alignment) })
82            .map(Alignment::from_raw)
83    }
84
85    /// Returns the sector that is closest to `sector`, satifies the `align` constraint, and lies
86    /// lies inside `geom`.
87    pub fn is_aligned(&self, geom: &Geometry, sector: i64) -> bool {
88        unsafe { ped_alignment_is_aligned(self.alignment, geom.geometry, sector) == 1 }
89    }
90
91    pub fn offset(&self) -> i64 {
92        unsafe { (*self.alignment).offset }
93    }
94}
95impl<'a> Drop for Alignment<'a> {
96    fn drop(&mut self) {
97        unsafe { ped_alignment_destroy(self.alignment) }
98    }
99}