Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profile: support owned profiles
Browse files Browse the repository at this point in the history
this is kind of weird but makes sense if you want to use test data
from raw bytes

Signed-off-by: William Casarin <jb55@jb55.com>
jb55 committed Apr 20, 2024
1 parent 608a7ad commit b6c5d8f
Showing 2 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/ndb.rs
Original file line number Diff line number Diff line change
@@ -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,
36 changes: 28 additions & 8 deletions src/profile.rs
Original file line number Diff line number Diff line change
@@ -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<ProfileRecord<'a>> {
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,

0 comments on commit b6c5d8f

Please sign in to comment.