1
0

feat: update cdylib

This commit is contained in:
2026-05-15 16:00:54 +08:00
parent 5c182bb4d3
commit 684a24fcf8
5 changed files with 202 additions and 20 deletions

46
Cargo.lock generated
View File

@@ -304,6 +304,28 @@ version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "num_enum"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d0bca838442ec211fa11de3a8b0e0e8f3a4522575b5c4c06ed722e005036f26"
dependencies = [
"num_enum_derive",
"rustversion",
]
[[package]]
name = "num_enum_derive"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8"
dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "once_cell"
version = "1.21.3"
@@ -339,6 +361,15 @@ dependencies = [
"windows-link",
]
[[package]]
name = "proc-macro-crate"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983"
dependencies = [
"toml_edit 0.23.9",
]
[[package]]
name = "proc-macro2"
version = "1.0.101"
@@ -567,7 +598,7 @@ dependencies = [
"serde",
"serde_spanned 0.6.9",
"toml_datetime 0.6.11",
"toml_edit",
"toml_edit 0.22.27",
]
[[package]]
@@ -617,6 +648,18 @@ dependencies = [
"winnow",
]
[[package]]
name = "toml_edit"
version = "0.23.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d7cbc3b4b49633d57a0509303158ca50de80ae32c265093b24c414705807832"
dependencies = [
"indexmap",
"toml_datetime 0.7.3",
"toml_parser",
"winnow",
]
[[package]]
name = "toml_parser"
version = "1.0.4"
@@ -765,6 +808,7 @@ name = "wfassoc-cdylib"
version = "0.1.0"
dependencies = [
"cbindgen",
"num_enum",
"slotmap",
"thiserror",
"wfassoc",

View File

@@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2022-2025 yyc12345
Copyright (c) 2022-2026 yyc12345
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -13,6 +13,7 @@ crate-type = ["cdylib"]
thiserror = { workspace = true }
wfassoc = { path="../wfassoc" }
slotmap = "1.1.1"
num_enum = "0.7.6"
[build-dependencies]
cbindgen = "0.29.0"

View File

@@ -2,6 +2,7 @@ mod cstr_ffi;
mod last_error;
mod object_pool;
use num_enum::TryFromPrimitive;
use object_pool::ObjectPool;
use std::sync::{LazyLock, RwLock};
use thiserror::Error as TeError;
@@ -32,6 +33,9 @@ enum Error {
#[error("{0}")]
ObjectPool(#[from] object_pool::Error),
/// Error occurs when checking enum value
#[error("")]
EnumOutOfRange,
/// Error when manipulating with poison RwLock
#[error("RwLock is poisoning")]
PoisonRwLock,
@@ -62,6 +66,12 @@ macro_rules! set_out_param {
};
}
macro_rules! resolve_enum {
($t:ty, $v:expr) => {
<$t>::try_from($v).map_err(|_| Error::EnumOutOfRange)
};
}
macro_rules! pull_reader {
($pool:expr) => {
$pool.read().map_err(|_| Error::PoisonRwLock)
@@ -173,6 +183,54 @@ macro_rules! cffi_wrapper {
// endregion
// region: Object Pools
static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
// endregion
// region: Exposed Types
#[repr(u32)]
#[derive(Debug, Copy, Clone, TryFromPrimitive)]
pub enum Scope {
User = 0,
System = 1,
}
impl From<Scope> for wfassoc::Scope {
fn from(value: Scope) -> Self {
match value {
Scope::User => wfassoc::Scope::User,
Scope::System => wfassoc::Scope::System,
}
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, TryFromPrimitive)]
pub enum View {
User,
System,
Hybrid,
}
impl From<View> for wfassoc::View {
fn from(value: View) -> Self {
match value {
View::User => wfassoc::View::User,
View::System => wfassoc::View::System,
View::Hybrid => wfassoc::View::Hybrid,
}
}
}
// endregion
// region: Exposed Functions
// region: Facilities
@@ -213,9 +271,6 @@ pub extern "C" fn WFHasPrivilege() -> bool {
// region: Schema
static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
cffi_wrapper!(|| -> (out_schema: Token) {
@@ -224,6 +279,14 @@ pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(Token)) -> bool {
cffi_wrapper!(|in_schema: Token| {
let mut pool = pull_writer!(SCHEMA_POOL)?;
Ok(pool.free(in_schema)?)
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaSetIdentifier(
in_schema: in_param_ty!(Token),
@@ -402,21 +465,10 @@ pub extern "C" fn WFSchemaAddExt(
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(Token)) -> bool {
cffi_wrapper!(|in_schema: Token| {
let mut pool = pull_writer!(SCHEMA_POOL)?;
Ok(pool.free(in_schema)?)
})
}
// endregion
// region: Program
static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramCreate(
in_schema: in_param_ty!(Token),
@@ -440,6 +492,78 @@ pub extern "C" fn WFProgramDestroy(in_program: in_param_ty!(Token)) -> bool {
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramRegister(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.register(scope.into())?;
Ok(())
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramUnregister(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.unregister(scope.into())?;
Ok(())
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramIsRegistered(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
out_is_registered: out_param_ty!(bool),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32| -> (out_is_registered: bool) {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
Ok(program.is_registered(scope.into())?)
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramLinkExt(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
in_index: in_param_ty!(usize),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32, in_index: usize| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.link_ext(scope.into(), in_index)?;
Ok(())
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramUnlinkExt(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
in_index: in_param_ty!(usize),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32, in_index: usize| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.unlink_ext(scope.into(), in_index)?;
Ok(())
})
}
// endregion
// endregion

View File

@@ -25,34 +25,40 @@ pub struct ObjectPool<T> {
}
impl<T> ObjectPool<T> {
/// Create a new [ObjectPool].
pub fn new() -> Self {
Self {
objs: SlotMap::new(),
}
}
/// Convert [slotmap] crate's [DefaultKey] to our [Token].
fn key_to_token(key: &DefaultKey) -> Token {
key.data().as_ffi()
}
/// Convert our [Token] to [slotmap] crate's [DefaultKey].
fn token_to_key(token: Token) -> DefaultKey {
DefaultKey::from(KeyData::from_ffi(token))
}
/// Put given object into the pool and return its token.
///
/// The ownership of given object is transferred to the pool.
pub fn allocate(&mut self, value: T) -> Result<Token, Error> {
let key = self.objs.insert(value);
Ok(Self::key_to_token(&key))
}
/// Free the object in the pool corresponding to the given token.
pub fn free(&mut self, token: Token) -> Result<(), Error> {
let _ = self.pop(token)?;
Ok(())
}
pub fn clear(&mut self) -> () {
self.objs.clear();
}
/// Remove the object from the pool corresponding to the given token and return it.
///
/// The ownership of the object is transferred to the caller.
pub fn pop(&mut self, token: Token) -> Result<T, Error> {
match self.objs.remove(Self::token_to_key(token)) {
Some(obj) => Ok(obj),
@@ -60,12 +66,19 @@ impl<T> ObjectPool<T> {
}
}
/// Clear all objects in the pool.
pub fn clear(&mut self) -> () {
self.objs.clear();
}
/// Get a reference to the object in the pool corresponding to the given token.
pub fn get(&self, token: Token) -> Result<&T, Error> {
self.objs
.get(Self::token_to_key(token))
.ok_or(Error::NoSuchToken)
}
/// Get a mutable reference to the object in the pool corresponding to the given token.
pub fn get_mut(&mut self, token: Token) -> Result<&mut T, Error> {
self.objs
.get_mut(Self::token_to_key(token))