hakuban 0.7.2

Data-object sharing library
Documentation
//if we don't mark pub function as unsafe when it has unsafe sections, clippy will complain
//if we put an unsafe block inside an unsafe function, clippy will complain too
//we'd rather have every unsafe block explicit, so disabling warning that function is not unsafe
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use log::{error, trace};

use super::ffi_result::FFIResult;
use crate::{ffi::ffi_result::FFIResultStatus, LocalExchange};

//TODO: capacity, limit

#[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()))
}