Skip to main content

pango/
tab_array.rs

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