diff --git a/src/ndb.rs b/src/ndb.rs index f0b1021..bfbd0df 100644 --- a/src/ndb.rs +++ b/src/ndb.rs @@ -201,7 +201,7 @@ impl Ndb { } // Convert the raw pointer to a Note instance - Ok(ProfileRecord::new( + Ok(ProfileRecord::new_transactional( profile_record_ptr, len, primkey, diff --git a/src/profile.rs b/src/profile.rs index 63e9300..851b066 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -1,14 +1,34 @@ -use crate::ndb_profile::{root_as_ndb_profile_record_unchecked, NdbProfileRecord}; -use crate::Transaction; +use crate::ndb_profile::{ + root_as_ndb_profile_record, root_as_ndb_profile_record_unchecked, NdbProfileRecord, +}; +use crate::{Error, Result, Transaction}; -pub struct ProfileRecord<'a> { - pub record: NdbProfileRecord<'a>, - pub primary_key: u64, - pub transaction: &'a Transaction, +pub enum ProfileRecord<'a> { + Transactional { + record: NdbProfileRecord<'a>, + primary_key: u64, + transaction: &'a Transaction, + }, + + Owned { + record: NdbProfileRecord<'a>, + }, } impl<'a> ProfileRecord<'a> { - pub(crate) fn new( + pub fn record(&self) -> NdbProfileRecord<'a> { + match self { + ProfileRecord::Transactional { record, .. } => *record, + ProfileRecord::Owned { record } => *record, + } + } + + pub fn new_owned(root: &'a [u8]) -> Result> { + let record = root_as_ndb_profile_record(root).map_err(|_| Error::DecodeError)?; + Ok(ProfileRecord::Owned { record }) + } + + pub(crate) fn new_transactional( ptr: *mut ::std::os::raw::c_void, len: usize, primary_key: u64, @@ -18,7 +38,7 @@ impl<'a> ProfileRecord<'a> { let bytes = std::slice::from_raw_parts(ptr as *const u8, len); root_as_ndb_profile_record_unchecked(bytes) }; - ProfileRecord { + ProfileRecord::Transactional { record, transaction, primary_key,