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
use crate::prelude::*;
use crate::Data;
use skia_bindings as sb;
use skia_bindings::SkFlattenable;
use std::ffi::CStr;

// TODO: getFactory()?
// TODO: NameToFactory()?
// TODO: FactoryToName()?
// TODO: Register()?
// TODO: getFlattenableType()?
// TODO: serialize() with SkSerialProcs?

pub trait Flattenable: Sized {
    fn type_name(&self) -> &CStr;
    fn serialize(&self) -> Data;
    fn deserialize(data: &[u8]) -> Option<Self>;
}

// TODO: find a way to hide these trait's functions from other crates.
pub trait NativeFlattenable {
    fn native_flattenable(&self) -> &SkFlattenable;
    fn native_deserialize(data: &[u8]) -> *mut Self;
}

impl<N> Flattenable for RCHandle<N>
where
    N: NativeFlattenable + NativeRefCountedBase,
{
    fn type_name(&self) -> &CStr {
        unsafe {
            CStr::from_ptr(sb::C_SkFlattenable_getTypeName(
                self.native().native_flattenable(),
            ))
        }
    }

    fn serialize(&self) -> Data {
        Data::from_ptr(unsafe { sb::C_SkFlattenable_serialize(self.native().native_flattenable()) })
            .unwrap()
    }

    fn deserialize(data: &[u8]) -> Option<Self> {
        RCHandle::from_ptr(N::native_deserialize(data))
    }
}