Skip to content

Commit

Permalink
Add Attribute::context member
Browse files Browse the repository at this point in the history
  • Loading branch information
rroelke committed Mar 18, 2024
1 parent 2463e7a commit b177eb2
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/array/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate tiledb_sys as ffi;

use std::convert::From;
use std::ops::Deref;

pub use tiledb_sys::Datatype;
Expand All @@ -9,61 +10,60 @@ use crate::error::Error;
use crate::filter_list::FilterList;
use crate::Result as TileDBResult;

pub(crate) struct RawAttribute {
ffi: *mut ffi::tiledb_attribute_t,
}

impl RawAttribute {
pub fn new(ffi: *mut ffi::tiledb_attribute_t) -> Self {
RawAttribute { ffi }
}
pub(crate) enum RawAttribute {
Owned(*mut ffi::tiledb_attribute_t),
}

impl Deref for RawAttribute {
type Target = *mut ffi::tiledb_attribute_t;
fn deref(&self) -> &Self::Target {
&self.ffi
let RawAttribute::Owned(ref ffi) = *self;
ffi
}
}

impl Drop for RawAttribute {
fn drop(&mut self) {
let RawAttribute::Owned(ref mut ffi) = *self;
unsafe {
ffi::tiledb_attribute_free(&mut self.ffi);
ffi::tiledb_attribute_free(ffi);
}
}
}

pub struct Attribute {
pub struct Attribute<'ctx> {
context: &'ctx Context,
raw: RawAttribute,
}

impl Attribute {
impl<'ctx> Attribute<'ctx> {
pub(crate) fn as_mut_ptr(&self) -> *mut ffi::tiledb_attribute_t {
*self.raw
}

pub fn new(
ctx: &Context,
context: &'ctx Context,
name: &str,
datatype: Datatype,
) -> TileDBResult<Attribute> {
) -> TileDBResult<Self> {
let c_context = context.as_mut_ptr();
let mut c_attr: *mut ffi::tiledb_attribute_t = out_ptr!();
let c_name = cstring!(name);
let res = unsafe {
ffi::tiledb_attribute_alloc(
ctx.as_mut_ptr(),
c_context,
c_name.as_c_str().as_ptr(),
datatype as u32,
&mut c_attr,
)
};
if res == ffi::TILEDB_OK {
Ok(Attribute {
raw: RawAttribute::new(c_attr),
context,
raw: RawAttribute::Owned(c_attr),
})
} else {
Err(ctx.expect_last_error())
Err(context.expect_last_error())
}
}

Expand Down Expand Up @@ -236,6 +236,15 @@ impl Attribute {
}
}

impl<'ctx> From<(&'ctx Context, RawAttribute)> for Attribute<'ctx> {
fn from(value: (&'ctx Context, RawAttribute)) -> Self {
Attribute {
context: value.0,
raw: value.1,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit b177eb2

Please sign in to comment.