#![allow(unsafe_code)]
use crate::hton::{DictInitMode, DictRecoveryMode};
use crate::panic_guard::FfiBoundary;
use crate::runtime;
use crate::runtime::FfiPtr;
fn report_u32(value: Option<u32>, out: *mut u32) -> bool {
match value {
Some(v) => {
if !out.is_null() {
unsafe { out.write(v) };
}
false
}
None => true,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__dict_init(mode: u32, version: u32) -> bool {
FfiBoundary::run_default(true, || match runtime::handlerton() {
Some(h) => match h.dict_init(DictInitMode::from_raw(mode), version) {
Ok(()) => false,
Err(_) => true,
},
None => false,
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__ddse_dict_init(mode: u32, version: u32) -> bool {
FfiBoundary::run_default(true, || match runtime::handlerton() {
Some(h) => match h.ddse_dict_init(DictInitMode::from_raw(mode), version) {
Ok(()) => false,
Err(_) => true,
},
None => false,
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__dict_register_dd_table_id(table_id: u64) {
FfiBoundary::run_void(|| {
if let Some(h) = runtime::handlerton() {
h.dict_register_dd_table_id(table_id);
}
});
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__dict_cache_reset(
schema: *const u8,
schema_len: usize,
table: *const u8,
table_len: usize,
) {
FfiBoundary::run_void(|| {
let schema_str = match unsafe { FfiPtr::bytes_to_str(schema, schema_len) } {
Ok(s) => s,
Err(_) => return,
};
let table_str = match unsafe { FfiPtr::bytes_to_str(table, table_len) } {
Ok(s) => s,
Err(_) => return,
};
if let Some(h) = runtime::handlerton() {
h.dict_cache_reset(schema_str, table_str);
}
});
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__dict_cache_reset_tables_and_tablespaces() {
FfiBoundary::run_void(|| {
if let Some(h) = runtime::handlerton() {
h.dict_cache_reset_tables_and_tablespaces();
}
});
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__dict_recover(mode: u32, version: u32) -> bool {
FfiBoundary::run_default(true, || match runtime::handlerton() {
Some(h) => match h.dict_recover(DictRecoveryMode::from_raw(mode), version) {
Ok(()) => false,
Err(_) => true,
},
None => false,
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__dict_get_server_version(out: *mut u32) -> bool {
FfiBoundary::run_default(true, || {
let value = match runtime::handlerton() {
Some(h) => h.dict_get_server_version(),
None => None,
};
report_u32(value, out)
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust__hton__dict_set_server_version() -> bool {
FfiBoundary::run_default(true, || match runtime::handlerton() {
Some(h) => match h.dict_set_server_version() {
Ok(()) => false,
Err(_) => true,
},
None => false,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn report_u32_some_writes_and_returns_false() {
let mut out: u32 = 0;
let res = report_u32(Some(42), &raw mut out);
assert!(!res);
assert_eq!(out, 42);
}
#[test]
fn report_u32_none_returns_true_without_writing() {
let mut out: u32 = 99;
let res = report_u32(None, &raw mut out);
assert!(res);
assert_eq!(out, 99);
}
#[test]
fn report_u32_tolerates_null_out() {
assert!(!report_u32(Some(0), core::ptr::null_mut()));
assert!(report_u32(None, core::ptr::null_mut()));
}
}