Skip to main content

draco_rs/
decode.rs

1use crate::{encode::EncoderBuffer, prelude::*};
2use autocxx::prelude::*;
3
4pub type DecoderBuffer = WrappedDracoObject<ffi::draco::DecoderBuffer>;
5
6impl DecoderBuffer {
7    pub fn new() -> Self {
8        Self(ffi::draco::DecoderBuffer::new().within_unique_ptr())
9    }
10
11    /// Initializes the decoder buffer with a raw data pointer and size
12    ///
13    /// # Safety
14    ///
15    /// The data pointer must be valid for the lifetime of the decoder buffer
16    pub unsafe fn from_buffer_ptr(data_ptr: *const std::os::raw::c_char, size: usize) -> Self {
17        let mut buffer = Self::new();
18        buffer.0.pin_mut().Init(data_ptr, size);
19        buffer
20    }
21
22    /// Initializes the decoder buffer with a slice of data
23    ///
24    /// # Safety
25    ///
26    /// The given slice must be valid for the lifetime of the decoder buffer
27    pub fn from_buffer(data_buffer: &[u8]) -> Self {
28        unsafe {
29            Self::from_buffer_ptr(
30                data_buffer.as_ptr() as *const std::os::raw::c_char,
31                data_buffer.len(),
32            )
33        }
34    }
35
36    /// Initializes the decoder buffer with the data from the encoder buffer
37    pub fn from_encoder_buffer(encoder_buffer: &mut EncoderBuffer) -> Self {
38        Self::from_buffer(encoder_buffer.as_slice())
39    }
40}
41
42impl Default for DecoderBuffer {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48pub struct Decoder {
49    pub(crate) decoder: UniquePtr<ffi::draco::Decoder>,
50}
51
52impl Decoder {
53    pub fn new() -> Self {
54        let decoder = ffi::draco::Decoder::new().within_unique_ptr();
55        Self { decoder }
56    }
57}
58impl Default for Decoder {
59    fn default() -> Self {
60        Self::new()
61    }
62}