pen_ffi/
string.rs

1use super::arc::ArcBuffer;
2use alloc::{string::String, vec::Vec};
3use core::hash::{Hash, Hasher};
4
5#[pen_ffi_macro::into_any(crate = "crate", into_fn = "pen_ffi_string_to_any")]
6#[repr(C)]
7#[derive(Clone, Debug, Default)]
8pub struct ByteString {
9    buffer: ArcBuffer,
10}
11
12impl ByteString {
13    pub fn as_slice(&self) -> &[u8] {
14        self.buffer.as_slice()
15    }
16}
17
18impl PartialEq for ByteString {
19    fn eq(&self, other: &Self) -> bool {
20        self.as_slice() == other.as_slice()
21    }
22}
23
24impl Eq for ByteString {}
25
26impl Hash for ByteString {
27    fn hash<H: Hasher>(&self, hasher: &mut H) {
28        self.as_slice().hash(hasher)
29    }
30}
31
32impl From<char> for ByteString {
33    fn from(character: char) -> Self {
34        String::from(character).into()
35    }
36}
37
38impl From<&[u8]> for ByteString {
39    fn from(bytes: &[u8]) -> Self {
40        Self {
41            buffer: bytes.into(),
42        }
43    }
44}
45
46impl From<&str> for ByteString {
47    fn from(string: &str) -> Self {
48        string.as_bytes().into()
49    }
50}
51
52impl From<String> for ByteString {
53    fn from(string: String) -> Self {
54        string.as_str().into()
55    }
56}
57
58// TODO Use Vec::into_raw_parts().
59impl From<Vec<u8>> for ByteString {
60    fn from(vec: Vec<u8>) -> Self {
61        vec.as_slice().into()
62    }
63}