Skip to content

Commit

Permalink
implement create Constant from i16 in c-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Alesfatalis committed May 20, 2024
1 parent 3f2a868 commit 4bf6985
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bindings/ergo-lib-c-core/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ pub unsafe fn constant_value_to_dbg_str(constant_ptr: ConstConstantPtr) -> Resul
Ok(s)
}

/// Create from i16 value
pub unsafe fn constant_from_i16(value: i16, constant_out: *mut ConstantPtr) -> Result<(), Error> {
let constant_out = mut_ptr_as_mut(constant_out, "constant_out")?;
*constant_out = Box::into_raw(Box::new(Constant(value.into())));
Ok(())
}

/// Extract i16 value, returning error if wrong type
pub unsafe fn constant_to_i16(constant_ptr: ConstConstantPtr) -> Result<i16, Error> {
let constant = const_ptr_as_ref(constant_ptr, "constant_ptr")?;
let i = i16::try_extract_from(constant.0.clone())?;
Ok(i)
}

/// Create from i32 value
pub unsafe fn constant_from_i32(value: i32, constant_out: *mut ConstantPtr) -> Result<(), Error> {
let constant_out = mut_ptr_as_mut(constant_out, "constant_out")?;
Expand Down
24 changes: 24 additions & 0 deletions bindings/ergo-lib-c/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ pub unsafe extern "C" fn ergo_lib_constant_value_to_dbg_str(
Error::c_api_from(res)
}

/// Create from i16 value
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_constant_from_i16(value: i16, constant_out: *mut ConstantPtr) {
#[allow(clippy::unwrap_used)]
constant_from_i16(value, constant_out).unwrap();
}

/// Extract i16 value, returning error if wrong type
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_constant_to_i16(
constant_ptr: ConstConstantPtr,
) -> ReturnNum<i16> {
match constant_to_i16(constant_ptr) {
Ok(value) => ReturnNum {
value,
error: std::ptr::null_mut(),
},
Err(e) => ReturnNum {
value: 0, // Just a dummy value
error: Error::c_api_from(Err(e)),
},
}
}

/// Create from i32 value
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_constant_from_i32(value: i32, constant_out: *mut ConstantPtr) {
Expand Down
1 change: 1 addition & 0 deletions bindings/ergo-lib-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub unsafe fn delete_ptr<T>(ptr: *mut T) {
pub trait IntegerType {}

impl IntegerType for u8 {}
impl IntegerType for i16 {}
impl IntegerType for i32 {}
impl IntegerType for u32 {}
impl IntegerType for i64 {}
Expand Down

0 comments on commit 4bf6985

Please sign in to comment.