pango/
tab_array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{translate::*, Slice};

use crate::{TabAlign, TabArray};

impl TabArray {
    /// If non-[`None`], @alignments and @locations are filled with allocated
    /// arrays.
    ///
    /// The arrays are of length [`size()`][Self::size()].
    /// You must free the returned array.
    ///
    /// # Returns
    ///
    ///
    /// ## `alignments`
    /// location to store an array of tab
    ///   stop alignments
    ///
    /// ## `locations`
    /// location to store an array
    ///   of tab positions
    #[doc(alias = "pango_tab_array_get_tabs")]
    #[doc(alias = "get_tabs")]
    pub fn tabs(&self) -> (Vec<TabAlign>, Slice<i32>) {
        let size = self.size() as usize;
        unsafe {
            let mut alignments = std::mem::MaybeUninit::uninit();
            let mut locations = std::mem::MaybeUninit::uninit();
            crate::ffi::pango_tab_array_get_tabs(
                mut_override(self.to_glib_none().0),
                alignments.as_mut_ptr(),
                locations.as_mut_ptr(),
            );
            let locations = Slice::from_glib_container_num(locations.assume_init(), size);
            let alignments = alignments.assume_init();
            let mut alignments_vec = Vec::with_capacity(locations.len());
            for i in 0..locations.len() {
                alignments_vec.push(from_glib(*alignments.add(i)));
            }
            (alignments_vec, locations)
        }
    }
}

#[cfg(feature = "v1_50")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
impl std::str::FromStr for TabArray {
    type Err = glib::BoolError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_string(s)
    }
}

#[cfg(test)]
mod tests {
    use crate::{TabAlign, TabArray};
    #[test]
    fn tab_array_tabs() {
        let mut array = TabArray::new(4, false);
        for i in 0..4 {
            array.set_tab(i, TabAlign::Left, i * 10);
        }
        let (alignments, locations) = array.tabs();
        assert_eq!(alignments.len(), 4);
        assert_eq!(locations.len(), 4);
        for i in 0..alignments.len() {
            assert_eq!(alignments[i], TabAlign::Left);
            assert_eq!(locations[i], i as i32 * 10);
        }
    }
}