skia_safe/interop/
strings.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
use crate::{
    interop::{FromStrs, String},
    prelude::*,
};
use skia_bindings::{self as sb, SkStrings};
use std::{fmt, ops::Index};

pub type Strings = Handle<SkStrings>;
unsafe_send_sync!(Strings);

impl NativeDrop for SkStrings {
    fn drop(&mut self) {
        unsafe {
            sb::C_SkStrings_destruct(self);
        }
    }
}

impl fmt::Debug for Strings {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Strings").field(&self.as_slice()).finish()
    }
}

impl Handle<SkStrings> {
    /// Constructs a native Strings array from a slice of SkStrings by moving them.
    pub fn new(mut strings: Vec<String>) -> Self {
        Strings::construct(|s| unsafe {
            sb::C_SkStrings_construct(s, strings.native_mut().as_mut_ptr(), strings.len())
        })
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        let mut count = 0;
        unsafe {
            sb::C_SkStrings_ptr_count(self.native(), &mut count);
        }
        count
    }

    pub fn as_slice(&self) -> &[String] {
        let mut count = 0;
        let ptr = unsafe { sb::C_SkStrings_ptr_count(self.native(), &mut count) };
        unsafe { safer::from_raw_parts(ptr as *const String, count) }
    }
}

impl Index<usize> for Strings {
    type Output = String;
    fn index(&self, index: usize) -> &Self::Output {
        &self.as_slice()[index]
    }
}

impl FromStrs for Strings {
    fn from_strs(strings: &[impl AsRef<str>]) -> Self {
        Strings::new(strings.iter().map(String::from_str).collect())
    }
}

#[test]
fn test_strings() {
    let strings = ["Hello", "World"];
    let strings = Strings::from_strs(&strings);
    assert_eq!(strings[0].as_str(), "Hello");
    assert_eq!(strings[1].as_str(), "World");
}