1#![allow(unused)]
2use crate::prelude::safer;
3use sb::TraitObject;
4use skia_bindings as sb;
5use std::{marker::PhantomData, mem};
6
7/// A sink / receiver for array data that is copied from C++ to a Rust [`Vec`].
8#[derive(Debug)]
9pub struct VecSink<'a, T> {
10 sink: sb::VecSink<T>,
11 pd: PhantomData<&'a mut Vec<T>>,
12}
13
14impl<T: 'static> VecSink<'_, T> {
15 /// Create a new sink that calls back into the closure given.
16 pub fn new(v: &mut dyn FnMut(&[T])) -> VecSink<T> {
17 VecSink {
18 sink: sb::VecSink {
19 fn_trait: unsafe { mem::transmute(v) },
20 set_fn: Some(Self::set_fn),
21 _phantom_0: PhantomData,
22 },
23 pd: PhantomData,
24 }
25 }
26
27 pub fn new_mut(v: &mut dyn FnMut(&mut [T])) -> VecSink<T> {
28 VecSink {
29 sink: sb::VecSink {
30 fn_trait: unsafe { mem::transmute(v) },
31 set_fn: Some(Self::set_fn_mut),
32 _phantom_0: PhantomData,
33 },
34 pd: PhantomData,
35 }
36 }
37
38 pub fn native_mut(&mut self) -> &mut sb::VecSink<T> {
39 &mut self.sink
40 }
41
42 unsafe extern "C" fn set_fn(ptr: *mut T, len: usize, rust_fn: TraitObject) {
43 let rust_fn: &mut dyn FnMut(&[T]) = mem::transmute(rust_fn);
44 (rust_fn)(safer::from_raw_parts(ptr, len));
45 }
46
47 unsafe extern "C" fn set_fn_mut(ptr: *mut T, len: usize, rust_fn: TraitObject) {
48 let rust_fn: &mut dyn FnMut(&mut [T]) = mem::transmute(rust_fn);
49 (rust_fn)(safer::from_raw_parts_mut(ptr, len));
50 }
51}
52
53#[derive(Debug)]
54pub struct Sink<'a, T> {
55 sink: sb::Sink<T>,
56 pd: PhantomData<&'a mut T>,
57}
58
59impl<T: 'static> Sink<'_, T> {
60 /// Create a new sink that calls back into the closure given.
61 pub fn new(v: &mut dyn FnMut(&T)) -> Sink<T> {
62 Sink {
63 sink: sb::Sink {
64 fn_trait: unsafe { mem::transmute(src:v) },
65 set_fn: Some(Self::set_fn),
66 _phantom_0: PhantomData,
67 },
68 pd: PhantomData,
69 }
70 }
71
72 pub fn native_mut(&mut self) -> &mut sb::Sink<T> {
73 &mut self.sink
74 }
75
76 unsafe extern "C" fn set_fn(ptr: *const T, rust_fn: TraitObject) {
77 let rust_fn: &mut dyn FnMut(&T) = mem::transmute(src:rust_fn);
78 (rust_fn)(&*ptr);
79 }
80}
81