#![allow(clippy::not_unsafe_ptr_arg_deref)]
use log::{error, trace};
use super::ffi_result::FFIResult;
use crate::{ffi::ffi_result::FFIResultStatus, LocalExchange};
#[no_mangle]
pub extern "C" fn hakuban_local_exchange_new(name_c: *const i8) -> FFIResult<*mut LocalExchange> {
let mut local_exchange_builder = LocalExchange::builder();
if !name_c.is_null() {
local_exchange_builder = local_exchange_builder.with_name(match unsafe { std::ffi::CStr::from_ptr(name_c).to_str() } {
Ok(string) => string,
Err(error) => {
error!("Couldn't convert exchange name parameter to string: {:?}", error);
return FFIResult::error(FFIResultStatus::InvalidString);
}
})
};
trace!("Constructing local exchange");
FFIResult::ok(Box::into_raw(Box::new(local_exchange_builder.build())))
}
#[no_mangle]
pub extern "C" fn hakuban_local_exchange_drop(local_exchange: *mut LocalExchange) {
let local_exchange = unsafe { Box::from_raw(local_exchange) };
trace!("Dropping local exchange {:?}", local_exchange);
drop(local_exchange);
}
#[no_mangle]
pub extern "C" fn hakuban_local_exchange_clone(local_exchange_pointer: *mut LocalExchange) -> *mut LocalExchange {
let local_exchange: &mut LocalExchange = unsafe { local_exchange_pointer.as_mut().unwrap() };
Box::into_raw(Box::new(local_exchange.clone()))
}