From b6c5d8fb9f3f79f14d69dd2d6eca2712e0f0a42d Mon Sep 17 00:00:00 2001 From: William Casarin Date: Fri, 19 Apr 2024 21:21:05 -0700 Subject: [PATCH] profile: support owned profiles this is kind of weird but makes sense if you want to use test data from raw bytes Signed-off-by: William Casarin --- src/ndb.rs | 2 +- src/profile.rs | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) 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,