rs_vips/
region.rs

1use crate::bindings::{self, _VipsBlob, vips_area_unref};
2
3#[derive(Debug, Clone)]
4pub struct VipsBlob {
5    pub(crate) ctx: *mut bindings::VipsBlob,
6}
7
8impl Drop for VipsBlob {
9    fn drop(&mut self) {
10        unsafe {
11            if !self
12                .ctx
13                .is_null()
14            {
15                bindings::g_object_unref(self.ctx as _);
16            }
17        }
18    }
19}
20
21impl From<*mut bindings::VipsBlob> for VipsBlob {
22    fn from(value: *mut bindings::VipsBlob) -> Self {
23        Self {
24            ctx: value,
25        }
26    }
27}
28
29#[allow(clippy::from_over_into)]
30impl Into<Vec<u8>> for VipsBlob {
31    fn into(self) -> Vec<u8> {
32        unsafe {
33            if self
34                .ctx
35                .is_null()
36            {
37                return Vec::new();
38            }
39
40            let mut size: u64 = 0;
41            let bytes = bindings::vips_blob_get(
42                self.ctx,
43                &mut size,
44            );
45            // unref area
46            unref_area(self.ctx);
47            Vec::from_raw_parts(
48                bytes as *mut u8,
49                size as usize,
50                size as usize,
51            )
52        }
53    }
54}
55
56pub(crate) fn unref_area(blob: *mut _VipsBlob) {
57    unsafe {
58        (*blob)
59            .area
60            .free_fn = None;
61        vips_area_unref(&mut (*blob).area);
62    }
63}