Skip to content

Commit d29b3c7

Browse files
authored
Merge pull request #178 from alexanderwiederin/ffi-as-mut-ptr
refactor(ffi): add `AsMutPtr` trait and replace implicit const-to-mut pointer cast
2 parents c6926ff + 398f712 commit d29b3c7

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/ffi/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ pub(crate) mod sealed {
99
fn as_ptr(&self) -> *const T;
1010
}
1111

12+
pub trait AsMutPtr<T> {
13+
/// Returns a raw mutable pointer to the underlying C object.
14+
fn as_mut_ptr(&self) -> *mut T;
15+
}
16+
1217
pub trait FromPtr<T> {
1318
/// Creates a wrapper from a raw const C pointer.
1419
unsafe fn from_ptr(ptr: *const T) -> Self;

src/notifications/types.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use libbitcoinkernel_sys::{
99
};
1010

1111
use crate::{
12-
ffi::sealed::{AsPtr, FromMutPtr, FromPtr},
12+
ffi::sealed::{AsMutPtr, AsPtr, FromMutPtr, FromPtr},
1313
BTCK_BLOCK_VALIDATION_RESULT_CACHED_INVALID, BTCK_BLOCK_VALIDATION_RESULT_CONSENSUS,
1414
BTCK_BLOCK_VALIDATION_RESULT_HEADER_LOW_WORK, BTCK_BLOCK_VALIDATION_RESULT_INVALID_HEADER,
1515
BTCK_BLOCK_VALIDATION_RESULT_INVALID_PREV, BTCK_BLOCK_VALIDATION_RESULT_MISSING_PREV,
@@ -214,6 +214,12 @@ impl AsPtr<btck_BlockValidationState> for BlockValidationState {
214214
}
215215
}
216216

217+
impl AsMutPtr<btck_BlockValidationState> for BlockValidationState {
218+
fn as_mut_ptr(&self) -> *mut btck_BlockValidationState {
219+
self.inner
220+
}
221+
}
222+
217223
impl FromMutPtr<btck_BlockValidationState> for BlockValidationState {
218224
unsafe fn from_ptr(ptr: *mut btck_BlockValidationState) -> Self {
219225
BlockValidationState { inner: ptr }

src/state/chainstate.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828
use std::ffi::CString;
2929

3030
use libbitcoinkernel_sys::{
31-
btck_BlockHash, btck_BlockValidationState, btck_ChainstateManager,
32-
btck_ChainstateManagerOptions, btck_block_read, btck_block_spent_outputs_read,
33-
btck_chainstate_manager_create, btck_chainstate_manager_destroy,
31+
btck_BlockHash, btck_ChainstateManager, btck_ChainstateManagerOptions, btck_block_read,
32+
btck_block_spent_outputs_read, btck_chainstate_manager_create, btck_chainstate_manager_destroy,
3433
btck_chainstate_manager_get_active_chain, btck_chainstate_manager_get_best_entry,
3534
btck_chainstate_manager_get_block_tree_entry_by_hash, btck_chainstate_manager_import_blocks,
3635
btck_chainstate_manager_options_create, btck_chainstate_manager_options_destroy,
@@ -45,7 +44,7 @@ use crate::{
4544
core::block::BlockHeader,
4645
ffi::{
4746
c_helpers,
48-
sealed::{AsPtr, FromMutPtr, FromPtr},
47+
sealed::{AsMutPtr, AsPtr, FromMutPtr, FromPtr},
4948
},
5049
notifications::types::BlockValidationState,
5150
Block, BlockHash, BlockSpentOutputs, BlockTreeEntry, KernelError,
@@ -311,7 +310,7 @@ impl ChainstateManager {
311310
btck_chainstate_manager_process_block_header(
312311
self.inner,
313312
header.as_ptr(),
314-
state.as_ptr() as *mut btck_BlockValidationState,
313+
state.as_mut_ptr(),
315314
)
316315
};
317316
if c_helpers::success(processed) {

0 commit comments

Comments
 (0)