Rust rewrite

This commit is contained in:
2024-06-14 18:22:20 +03:00
parent dc7f72f6ef
commit 35cb20545c
30 changed files with 1117 additions and 766 deletions

View File

@@ -0,0 +1,12 @@
[package]
name = "dim_plugin_helper"
version = "0.1.0"
edition = "2021"
[lib]
name="dim_plugin_helper"
[dependencies]
bytes = "1.6.0"
lazy_static = { version = "1.4.0", features = ["spin"] }
libc = "0.2.155"

View File

@@ -0,0 +1,111 @@
use std::{ffi::{c_char, c_void, CStr, CString}, io::Write};
pub use libc::snprintf;
#[repr(C)]
#[derive(Debug)]
pub struct PluginInfo {
ptrs: PluginInfoPtrs,
c_name: CString,
c_version: CString,
c_license: CString
}
#[repr(C)]
#[derive(Debug)]
struct PluginInfoPtrs {
name: *const c_char,
version: *const c_char,
license: *const c_char,
}
impl PluginInfo {
// pub fn new() -> Self {
// Self {
// name: std::ptr::null(),
// version: std::ptr::null(),
// license: std::ptr::null(),
// c_name: Default::default(),
// c_version: Default::default(),
// c_license: Default::default()
// }
//}
pub fn new(name: &str, version: &str, license: &str) -> Self {
let c_name = CString::new(name).unwrap();
let c_version = CString::new(version).unwrap();
let c_license = CString::new(license).unwrap();
let name = c_name.as_ptr();
let version = if c_version.is_empty() {
std::ptr::null()
} else {c_version.as_ptr()};
let license = if c_license.is_empty() {
std::ptr::null()
} else {c_license.as_ptr()};
Self { ptrs: PluginInfoPtrs{ name, version, license }, c_name, c_version, c_license }
}
pub fn get(&self) -> *const c_void {
&self.ptrs as *const _ as *const c_void
}
}
unsafe impl Sync for PluginInfo {}
// Lord have mercy on me
#[macro_export]
macro_rules! plugin_info {
($name:literal, $version:literal, $license:literal) => {
lazy_static::lazy_static!(
static ref PLUGIN_INFO: PluginInfo = PluginInfo::new($name, $version, $license);
);
};
}
#[derive(Debug)]
pub struct CBuffer {
inner: *mut i8,
count: usize,
capacity: usize
}
impl CBuffer {
pub fn from_raw_parts_mut(buf: *mut i8, capacity: usize) -> Self {
Self {
inner: buf,
capacity,
count: 0,
}
}
}
impl Write for CBuffer {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let mut count = 0;
for c in buf {
unsafe {
if self.count + count >= self.capacity - 1 {
return Err(std::io::ErrorKind::OutOfMemory.into());
}
(*self.inner.add(self.count + count)) = *c as i8;
}
count += 1;
}
unsafe {
(*self.inner.add(self.count + count)) = 0;
}
self.count += count;
Ok(count as usize)
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}