skia_safe/interop/
string.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![allow(dead_code)]
use std::{fmt, str};

use crate::prelude::*;
use skia_bindings::{self as sb, SkString};

pub type String = Handle<SkString>;
unsafe_send_sync!(String);

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

impl NativeClone for SkString {
    fn clone(&self) -> Self {
        construct(|unitialized| unsafe { sb::C_SkString_CopyConstruct(unitialized, self) })
    }
}

impl AsRef<str> for String {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for String {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl Default for String {
    fn default() -> Self {
        Self::from_str("")
    }
}

impl fmt::Debug for String {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl String {
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(str: impl AsRef<str>) -> String {
        let bytes = str.as_ref().as_bytes();
        Handle::from_native_c(unsafe { SkString::new3(bytes.as_ptr() as _, bytes.len()) })
    }

    pub fn as_str(&self) -> &str {
        self.native().as_str()
    }
}

pub trait AsStr {
    fn as_str(&self) -> &str;
}

impl AsStr for Handle<SkString> {
    fn as_str(&self) -> &str {
        self.native().as_str()
    }
}

impl AsStr for SkString {
    fn as_str(&self) -> &str {
        let mut size = 0;
        let slice = unsafe {
            let ptr = sb::C_SkString_c_str_size(self, &mut size) as *const u8;
            safer::from_raw_parts(ptr, size)
        };
        std::str::from_utf8(slice).unwrap_or_default()
    }
}

impl AsStr for sb::std_string_view {
    fn as_str(&self) -> &str {
        let slice = unsafe {
            let mut size = 0;
            let ptr = sb::C_string_view_ptr_size(self, &mut size) as *const u8;
            safer::from_raw_parts(ptr, size)
        };
        str::from_utf8(slice).unwrap_or_default()
    }
}

impl AsStr for sb::std_string {
    fn as_str(&self) -> &str {
        let slice = unsafe {
            let mut size = 0;
            let ptr = sb::C_string_ptr_size(self, &mut size) as *const u8;
            safer::from_raw_parts(ptr, size)
        };
        str::from_utf8(slice).unwrap_or_default()
    }
}

pub trait SetStr {
    fn set_str(&mut self, str: impl AsRef<str>);
}

impl SetStr for Handle<SkString> {
    fn set_str(&mut self, str: impl AsRef<str>) {
        self.native_mut().set_str(str)
    }
}

impl SetStr for SkString {
    fn set_str(&mut self, str: impl AsRef<str>) {
        let bytes = str.as_ref().as_bytes();
        unsafe { self.set1(bytes.as_ptr() as _, bytes.len()) }
    }
}

pub trait FromStrs {
    fn from_strs(strings: &[impl AsRef<str>]) -> Self;
}

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

#[cfg(test)]
mod tests {
    use super::{SetStr, String};

    #[test]
    fn string_from_rust_and_back() {
        let str = "Hello";
        let string = String::from_str(str);
        assert_eq!(str, string.as_str())
    }

    #[test]
    fn set_string() {
        let mut hello = String::from_str("Hello");
        hello.set_str(String::from_str("World"));
        assert_eq!("World", hello.as_str());
    }
}