pango/
tab_array.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{translate::*, Slice};
4
5use crate::{TabAlign, TabArray};
6
7impl TabArray {
8    /// If non-[`None`], @alignments and @locations are filled with allocated
9    /// arrays.
10    ///
11    /// The arrays are of length [`size()`][Self::size()].
12    /// You must free the returned array.
13    ///
14    /// # Returns
15    ///
16    ///
17    /// ## `alignments`
18    /// location to store an array of tab
19    ///   stop alignments
20    ///
21    /// ## `locations`
22    /// location to store an array
23    ///   of tab positions
24    #[doc(alias = "pango_tab_array_get_tabs")]
25    #[doc(alias = "get_tabs")]
26    pub fn tabs(&self) -> (Vec<TabAlign>, Slice<i32>) {
27        let size = self.size() as usize;
28        unsafe {
29            let mut alignments = std::mem::MaybeUninit::uninit();
30            let mut locations = std::mem::MaybeUninit::uninit();
31            crate::ffi::pango_tab_array_get_tabs(
32                mut_override(self.to_glib_none().0),
33                alignments.as_mut_ptr(),
34                locations.as_mut_ptr(),
35            );
36            let locations = Slice::from_glib_container_num(locations.assume_init(), size);
37            let alignments = alignments.assume_init();
38            let mut alignments_vec = Vec::with_capacity(locations.len());
39            for i in 0..locations.len() {
40                alignments_vec.push(from_glib(*alignments.add(i)));
41            }
42            (alignments_vec, locations)
43        }
44    }
45}
46
47#[cfg(feature = "v1_50")]
48#[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
49impl std::str::FromStr for TabArray {
50    type Err = glib::BoolError;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        Self::from_string(s)
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use crate::{TabAlign, TabArray};
60    #[test]
61    fn tab_array_tabs() {
62        let mut array = TabArray::new(4, false);
63        for i in 0..4 {
64            array.set_tab(i, TabAlign::Left, i * 10);
65        }
66        let (alignments, locations) = array.tabs();
67        assert_eq!(alignments.len(), 4);
68        assert_eq!(locations.len(), 4);
69        for i in 0..alignments.len() {
70            assert_eq!(alignments[i], TabAlign::Left);
71            assert_eq!(locations[i], i as i32 * 10);
72        }
73    }
74}