use std::marker::PhantomData;
use std::mem::MaybeUninit;
use crate::sql::{mcosql_error_code, result_from_code};
use crate::{exdb_sys, Result};
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Ref<'a> {
owner: PhantomData<&'a ()>,
pub(crate) h: exdb_sys::mcosql_rs_allocator,
}
impl<'a> Ref<'a> {
pub(crate) fn new(alloc: &'a Owned) -> Self {
Self::from_handle(alloc.h, alloc)
}
pub(crate) fn from_handle<T>(h: exdb_sys::mcosql_rs_allocator, _owner: &'a T) -> Self {
Ref {
owner: PhantomData,
h,
}
}
}
pub(crate) struct Owned {
pub(crate) h: exdb_sys::mcosql_rs_allocator,
}
impl Owned {
pub(crate) fn new() -> Result<Self> {
let mut h = MaybeUninit::uninit();
result_from_code(unsafe { exdb_sys::mcosql_rs_allocator_create(h.as_mut_ptr()) }).and(Ok(
Owned {
h: unsafe { h.assume_init() },
},
))
}
}
impl Drop for Owned {
fn drop(&mut self) {
let rc = unsafe { exdb_sys::mcosql_rs_allocator_destroy(self.h) };
debug_assert_eq!(mcosql_error_code::SQL_OK, rc);
}
}