Skip to content

Commit 248703d

Browse files
committed
Add unowned block type
1 parent b30f00f commit 248703d

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/lib.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ impl From<kernel_BlockValidationResult> for BlockValidationResult {
507507
}
508508

509509
/// Exposes the result after validating a block.
510-
pub trait VIBlockCheckedFn: Fn(ValidationMode, BlockValidationResult) {}
511-
impl<F: Fn(ValidationMode, BlockValidationResult)> VIBlockCheckedFn for F {}
510+
pub trait VIBlockCheckedFn: Fn(UnownedBlock, ValidationMode, BlockValidationResult) {}
511+
impl<F: Fn(UnownedBlock, ValidationMode, BlockValidationResult)> VIBlockCheckedFn for F {}
512512

513513
/// A holder struct for validation interface callbacks
514514
pub struct ValidationInterfaceCallbackHolder {
@@ -518,13 +518,13 @@ pub struct ValidationInterfaceCallbackHolder {
518518

519519
unsafe extern "C" fn vi_block_checked_wrapper(
520520
user_data: *mut c_void,
521-
_: *const kernel_BlockPointer,
521+
block: *const kernel_BlockPointer,
522522
stateIn: *const kernel_BlockValidationState,
523523
) {
524524
let holder = &*(user_data as *mut ValidationInterfaceCallbackHolder);
525525
let result = kernel_get_block_validation_result_from_block_validation_state(stateIn);
526526
let mode = kernel_get_validation_mode_from_block_validation_state(stateIn);
527-
(holder.block_checked)(mode.into(), result.into());
527+
(holder.block_checked)(UnownedBlock::new(block), mode.into(), result.into());
528528
}
529529

530530
/// A wrapper for the validation interface. This is the struct that has to be
@@ -703,6 +703,29 @@ impl Drop for Transaction {
703703
}
704704
}
705705

706+
/// A single unowned block. Can only be used for copying data from it.
707+
pub struct UnownedBlock {
708+
inner: *const kernel_BlockPointer,
709+
}
710+
711+
impl UnownedBlock {
712+
fn new(block: *const kernel_BlockPointer) -> UnownedBlock {
713+
UnownedBlock{ inner: block }
714+
}
715+
}
716+
717+
impl Into<Vec<u8>> for UnownedBlock {
718+
fn into(self) -> Vec<u8> {
719+
let raw_block = unsafe { kernel_copy_block_pointer_data(self.inner) };
720+
let vec = unsafe {
721+
std::slice::from_raw_parts((*raw_block).data, (*raw_block).size.try_into().unwrap())
722+
}
723+
.to_vec();
724+
unsafe { kernel_byte_array_destroy(raw_block) };
725+
vec
726+
}
727+
}
728+
706729
/// A single Block
707730
pub struct Block {
708731
inner: *mut kernel_Block,

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod tests {
7070
fn setup_validation_interface(context: &Context) -> ValidationInterfaceWrapper {
7171
let validation_interface =
7272
ValidationInterfaceWrapper::new(Box::new(ValidationInterfaceCallbackHolder {
73-
block_checked: Box::new(|_mode, _result| {
73+
block_checked: Box::new(|_block, _mode, _result| {
7474
log::info!("Block checked!");
7575
}),
7676
}));

0 commit comments

Comments
 (0)