Minor improvements.

This commit is contained in:
Alejandro Cabeza Romero 2026-04-24 12:49:17 +02:00
parent 743895bfc7
commit 4c0e77d70a
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
3 changed files with 19 additions and 10 deletions

View File

@ -1,6 +1,7 @@
use std::ffi::c_char;
use crate::ffi::ConstBytes;
#[repr(C)]
pub struct WitnessInput {
pub dat: ConstBytes,
pub inputs_json: *const c_char,

View File

@ -4,11 +4,22 @@ pub struct Bytes(Vec<u8>);
impl Bytes {
#[must_use]
pub fn inner(&self) -> &Vec<u8> {
pub fn into_inner(self) -> Vec<u8> {
self.0
}
#[must_use]
pub fn as_slice(&self) -> &[u8] {
&self.0
}
}
impl From<Vec<u8>> for Bytes {
fn from(value: Vec<u8>) -> Self {
Self(value)
}
}
impl From<ffi::Bytes> for Bytes {
fn from(mut ffi_value: ffi::Bytes) -> Self {
let raw = unsafe {

View File

@ -1,4 +1,4 @@
use std::ffi::{c_char, CString};
use std::ffi::{c_char, CString, NulError};
use crate::ffi;
pub struct WitnessInput {
@ -12,8 +12,7 @@ impl WitnessInput {
Self { dat, inputs_json }
}
#[must_use]
pub fn as_ffi(&'_ self) -> WitnessInputFfiGuard<'_> {
pub fn as_ffi(&'_ self) -> Result<WitnessInputFfiGuard<'_>, NulError> {
WitnessInputFfiGuard::new(self)
}
}
@ -28,15 +27,14 @@ pub struct WitnessInputFfiGuard<'a> {
}
impl<'a> WitnessInputFfiGuard<'a> {
#[must_use]
fn new(inner: &'a WitnessInput) -> Self {
fn new(inner: &'a WitnessInput) -> Result<Self, NulError> {
let dat = ffi::ConstBytes { data: inner.dat.as_ptr(), size: inner.dat.len() };
let inputs_json = CString::new(inner.inputs_json.clone()).expect("CString::new failed").into_raw();
let inputs_json = CString::new(inner.inputs_json.clone())?.into_raw();
let ffi = ffi::WitnessInput { dat, inputs_json };
Self {
Ok(Self {
ffi,
_lifetime: std::marker::PhantomData,
}
})
}
}
@ -55,4 +53,3 @@ impl<'a> Drop for WitnessInputFfiGuard<'a> {
}
}
}