84 lines
2.0 KiB
Rust
Raw Normal View History

2023-05-30 22:23:11 -07:00
#![allow(clippy::missing_safety_doc)]
2022-07-05 21:13:09 -07:00
#![feature(format_args_nl)]
#![feature(io_error_more)]
#![feature(utf8_chunks)]
2022-07-05 21:13:09 -07:00
pub use const_format;
2022-08-08 22:53:37 -07:00
pub use libc;
2023-10-13 16:59:54 -07:00
use num_traits::FromPrimitive;
2022-08-19 02:21:52 -07:00
pub use cstr::*;
2023-06-09 02:00:37 -07:00
use cxx_extern::*;
2022-09-15 01:17:05 -07:00
pub use files::*;
2022-07-01 04:53:41 -07:00
pub use logging::*;
2022-07-05 21:13:09 -07:00
pub use misc::*;
2024-03-05 01:48:35 -08:00
pub use result::*;
2022-07-01 04:53:41 -07:00
mod cstr;
2023-06-09 02:00:37 -07:00
mod cxx_extern;
2022-09-15 01:17:05 -07:00
mod files;
2022-07-01 04:53:41 -07:00
mod logging;
2022-07-05 21:13:09 -07:00
mod misc;
2024-03-05 01:48:35 -08:00
mod result;
2022-08-08 22:53:37 -07:00
mod xwrap;
2022-07-01 04:53:41 -07:00
#[cxx::bridge]
pub mod ffi {
2022-07-06 01:16:08 -07:00
#[derive(Copy, Clone)]
2023-10-13 16:59:54 -07:00
#[repr(i32)]
#[cxx_name = "LogLevel"]
pub(crate) enum LogLevelCxx {
ErrorCxx,
2022-07-01 04:53:41 -07:00
Error,
Warn,
Info,
Debug,
}
2023-06-20 18:17:26 -07:00
unsafe extern "C++" {
include!("misc.hpp");
2023-12-26 23:08:06 +08:00
#[namespace = "rust"]
#[cxx_name = "Utf8CStr"]
type Utf8CStrRef<'a> = &'a crate::cstr::Utf8CStr;
2023-06-20 18:17:26 -07:00
fn mut_u8_patch(buf: &mut [u8], from: &[u8], to: &[u8]) -> Vec<usize>;
}
2022-07-01 04:53:41 -07:00
extern "Rust" {
2023-08-24 00:50:38 -07:00
#[cxx_name = "log_with_rs"]
2023-12-26 23:08:06 +08:00
fn log_from_cxx(level: LogLevelCxx, msg: Utf8CStrRef);
2023-10-13 16:59:54 -07:00
#[cxx_name = "set_log_level_state"]
fn set_log_level_state_cxx(level: LogLevelCxx, enabled: bool);
2022-07-01 04:53:41 -07:00
fn exit_on_error(b: bool);
2022-07-06 01:16:08 -07:00
fn cmdline_logging();
2023-12-08 23:30:55 +08:00
fn resize_vec(vec: &mut Vec<u8>, size: usize);
2022-07-01 04:53:41 -07:00
}
2022-08-19 02:21:52 -07:00
2023-05-16 02:09:05 -07:00
#[namespace = "rust"]
2022-08-19 02:21:52 -07:00
extern "Rust" {
fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32;
2023-08-10 21:22:53 -07:00
#[cxx_name = "fd_path"]
fn fd_path_for_cxx(fd: i32, buf: &mut [u8]) -> isize;
#[cxx_name = "map_file"]
2023-12-26 23:08:06 +08:00
fn map_file_for_cxx(path: Utf8CStrRef, rw: bool) -> &'static mut [u8];
2023-08-10 21:22:53 -07:00
#[cxx_name = "map_fd"]
fn map_fd_for_cxx(fd: i32, sz: usize, rw: bool) -> &'static mut [u8];
2022-08-19 02:21:52 -07:00
}
}
2023-10-13 16:59:54 -07:00
fn set_log_level_state_cxx(level: ffi::LogLevelCxx, enabled: bool) {
if let Some(level) = LogLevel::from_i32(level.repr) {
set_log_level_state(level, enabled)
}
}
2023-12-08 23:30:55 +08:00
fn resize_vec(vec: &mut Vec<u8>, size: usize) {
if size > vec.len() {
vec.reserve(size - vec.len());
}
unsafe {
vec.set_len(size);
}
2023-12-08 23:30:55 +08:00
}