Remove unneeded pointers from FfiResult types
This commit is contained in:
parent
a056c0dd59
commit
3e31e9aca3
15
src/types.rs
15
src/types.rs
@ -5,15 +5,8 @@ use safer_ffi::char_p::char_p_boxed;
|
|||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FfiResult<T> {
|
pub struct FfiResult<T> {
|
||||||
pub ok: Option<repr_c::Box<T>>,
|
pub ok: T,
|
||||||
pub err: Option<repr_c::Box<char_p_boxed>>,
|
pub err: char_p_boxed,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive_ReprC]
|
|
||||||
#[repr(C)]
|
|
||||||
pub struct FfiResultVec<T> {
|
|
||||||
pub ok: repr_c::Vec<T>,
|
|
||||||
pub err: Option<repr_c::Box<char_p_boxed>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
@ -22,8 +15,8 @@ fn free_string_result(string_result: FfiResult<char_p_boxed>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
fn free_void_result(void_result: FfiResult<()>) {
|
fn free_int_result(int_result: FfiResult<i32>) {
|
||||||
drop(void_result)
|
drop(int_result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO do we need this? remove?
|
// TODO do we need this? remove?
|
||||||
|
@ -13,7 +13,8 @@ use blockchain::BlockchainConfig;
|
|||||||
use database::DatabaseConfig;
|
use database::DatabaseConfig;
|
||||||
|
|
||||||
use crate::error::get_name;
|
use crate::error::get_name;
|
||||||
use crate::types::{FfiResult, FfiResultVec};
|
use crate::types::FfiResult;
|
||||||
|
use std::ffi::CString;
|
||||||
|
|
||||||
mod blockchain;
|
mod blockchain;
|
||||||
mod database;
|
mod database;
|
||||||
@ -32,7 +33,7 @@ fn new_wallet_result(
|
|||||||
change_descriptor: Option<char_p_ref>,
|
change_descriptor: Option<char_p_ref>,
|
||||||
blockchain_config: &BlockchainConfig,
|
blockchain_config: &BlockchainConfig,
|
||||||
database_config: &DatabaseConfig,
|
database_config: &DatabaseConfig,
|
||||||
) -> FfiResult<OpaqueWallet> {
|
) -> FfiResult<Option<Box<OpaqueWallet>>> {
|
||||||
let descriptor = descriptor.to_string();
|
let descriptor = descriptor.to_string();
|
||||||
let change_descriptor = change_descriptor.map(|s| s.to_string());
|
let change_descriptor = change_descriptor.map(|s| s.to_string());
|
||||||
let bc_config = &blockchain_config.raw;
|
let bc_config = &blockchain_config.raw;
|
||||||
@ -42,11 +43,11 @@ fn new_wallet_result(
|
|||||||
match wallet_result {
|
match wallet_result {
|
||||||
Ok(w) => FfiResult {
|
Ok(w) => FfiResult {
|
||||||
ok: Some(Box::new(OpaqueWallet { raw: w })),
|
ok: Some(Box::new(OpaqueWallet { raw: w })),
|
||||||
err: None,
|
err: char_p_boxed::from(CString::default()),
|
||||||
},
|
},
|
||||||
Err(e) => FfiResult {
|
Err(e) => FfiResult {
|
||||||
ok: None,
|
ok: None,
|
||||||
err: Some(Box::new(char_p_boxed::try_from(get_name(&e)).unwrap())),
|
err: char_p_boxed::try_from(get_name(&e)).unwrap(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,23 +70,23 @@ fn new_wallet(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
fn free_wallet_result(wallet_result: FfiResult<OpaqueWallet>) {
|
fn free_wallet_result(wallet_result: FfiResult<Option<Box<OpaqueWallet>>>) {
|
||||||
drop(wallet_result);
|
drop(wallet_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// wallet operations
|
// wallet operations
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
fn sync_wallet(opaque_wallet: &OpaqueWallet) -> FfiResult<()> {
|
fn sync_wallet(opaque_wallet: &OpaqueWallet) -> FfiResult<i32> {
|
||||||
let void_result = opaque_wallet.raw.sync(log_progress(), Some(100));
|
let int_result = opaque_wallet.raw.sync(log_progress(), Some(100));
|
||||||
match void_result {
|
match int_result {
|
||||||
Ok(v) => FfiResult {
|
Ok(_v) => FfiResult {
|
||||||
ok: Some(Box::new(v)),
|
ok: 0,
|
||||||
err: None,
|
err: char_p_boxed::from(CString::default()),
|
||||||
},
|
},
|
||||||
Err(e) => FfiResult {
|
Err(e) => FfiResult {
|
||||||
ok: None,
|
ok: -1,
|
||||||
err: Some(Box::new(char_p_boxed::try_from(get_name(&e)).unwrap())),
|
err: char_p_boxed::try_from(get_name(&e)).unwrap(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,37 +97,37 @@ fn new_address(opaque_wallet: &OpaqueWallet) -> FfiResult<char_p_boxed> {
|
|||||||
let string_result = new_address.map(|a| a.to_string());
|
let string_result = new_address.map(|a| a.to_string());
|
||||||
match string_result {
|
match string_result {
|
||||||
Ok(a) => FfiResult {
|
Ok(a) => FfiResult {
|
||||||
ok: Some(Box::new(char_p_boxed::try_from(a).unwrap())),
|
ok: char_p_boxed::try_from(a).unwrap(),
|
||||||
err: None,
|
err: char_p_boxed::from(CString::default()),
|
||||||
},
|
},
|
||||||
Err(e) => FfiResult {
|
Err(e) => FfiResult {
|
||||||
ok: None,
|
ok: char_p_boxed::from(CString::default()),
|
||||||
err: Some(Box::new(char_p_boxed::try_from(get_name(&e)).unwrap())),
|
err: char_p_boxed::try_from(get_name(&e)).unwrap(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
fn list_unspent(opaque_wallet: &OpaqueWallet) -> FfiResultVec<LocalUtxo> {
|
fn list_unspent(opaque_wallet: &OpaqueWallet) -> FfiResult<repr_c::Vec<LocalUtxo>> {
|
||||||
let unspent_result = opaque_wallet.raw.list_unspent();
|
let unspent_result = opaque_wallet.raw.list_unspent();
|
||||||
|
|
||||||
match unspent_result {
|
match unspent_result {
|
||||||
Ok(v) => FfiResultVec {
|
Ok(v) => FfiResult {
|
||||||
ok: {
|
ok: {
|
||||||
let ve: Vec<LocalUtxo> = v.iter().map(|lu| LocalUtxo::from(lu)).collect();
|
let ve: Vec<LocalUtxo> = v.iter().map(|lu| LocalUtxo::from(lu)).collect();
|
||||||
repr_c::Vec::from(ve)
|
repr_c::Vec::from(ve)
|
||||||
},
|
},
|
||||||
err: None,
|
err: char_p_boxed::from(CString::default()),
|
||||||
},
|
},
|
||||||
Err(e) => FfiResultVec {
|
Err(e) => FfiResult {
|
||||||
ok: repr_c::Vec::EMPTY,
|
ok: repr_c::Vec::EMPTY,
|
||||||
err: Some(Box::new(char_p_boxed::try_from(get_name(&e)).unwrap())),
|
err: char_p_boxed::try_from(get_name(&e)).unwrap(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
fn free_unspent_result(unspent_result: FfiResultVec<LocalUtxo>) {
|
fn free_unspent_result(unspent_result: FfiResult<repr_c::Vec<LocalUtxo>>) {
|
||||||
drop(unspent_result)
|
drop(unspent_result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
test.sh
4
test.sh
@ -6,8 +6,8 @@ cargo test --features c-headers -- generate_headers
|
|||||||
|
|
||||||
# cc
|
# cc
|
||||||
export LD_LIBRARY_PATH=`pwd`/target/debug
|
export LD_LIBRARY_PATH=`pwd`/target/debug
|
||||||
valgrind --leak-check=full --show-leak-kinds=all cc/bdk_ffi_test
|
#valgrind --leak-check=full --show-leak-kinds=all cc/bdk_ffi_test
|
||||||
#cc/bdk_ffi_test
|
cc/bdk_ffi_test
|
||||||
|
|
||||||
# bdk-kotlin
|
# bdk-kotlin
|
||||||
(cd bdk-kotlin && gradle test)
|
(cd bdk-kotlin && gradle test)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user