lwgeom 0.0.5

Rust bindings for the PostGIS LWGEOM geometry library.
Documentation
use core::ffi::CStr;
use core::fmt;
use core::ptr::NonNull;

use lwgeom_sys::*;

use crate::{LWGeomError, Result};

pub struct GBox {
    inner: NonNull<GBOX>,
}

impl GBox {
    pub(crate) fn new_from_raw(p_gbox: *mut GBOX) -> Result<Self> {
        Ok(Self {
            inner: NonNull::new(p_gbox).ok_or(LWGeomError::NullPtrError)?,
        })
    }

    pub fn new_bbox() -> Result<Self> {
        let p_gbox = unsafe { gbox_new(LWFLAG_BBOX as lwflags_t) };
        Self::new_from_raw(p_gbox)
    }

    fn inner_ptr(&self) -> *mut GBOX {
        self.inner.as_ptr()
    }

    fn inner_ref(&self) -> &GBOX {
        unsafe { self.inner.as_ref() }
    }

    pub fn xmin(&self) -> f64 {
        self.inner_ref().xmin
    }

    pub fn xmax(&self) -> f64 {
        self.inner_ref().xmax
    }

    pub fn ymin(&self) -> f64 {
        self.inner_ref().ymin
    }

    pub fn ymax(&self) -> f64 {
        self.inner_ref().ymax
    }
}

impl fmt::Display for GBox {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let c_gbox_string = unsafe { CStr::from_ptr(gbox_to_string(self.inner_ptr())) };
        write!(f, "{}", c_gbox_string.to_string_lossy())
    }
}

unsafe impl Send for GBox {}
unsafe impl Sync for GBox {}

// impl Drop for GBox {
//     fn drop(&mut self) {
//         unsafe { lwfree(self.inner_ptr() as *mut c_void) }
//     }
// }