feat: update cdylib
This commit is contained in:
46
Cargo.lock
generated
46
Cargo.lock
generated
@@ -304,6 +304,28 @@ version = "2.7.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
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]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.21.3"
|
version = "1.21.3"
|
||||||
@@ -339,6 +361,15 @@ dependencies = [
|
|||||||
"windows-link",
|
"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]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.101"
|
version = "1.0.101"
|
||||||
@@ -567,7 +598,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"serde_spanned 0.6.9",
|
"serde_spanned 0.6.9",
|
||||||
"toml_datetime 0.6.11",
|
"toml_datetime 0.6.11",
|
||||||
"toml_edit",
|
"toml_edit 0.22.27",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -617,6 +648,18 @@ dependencies = [
|
|||||||
"winnow",
|
"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]]
|
[[package]]
|
||||||
name = "toml_parser"
|
name = "toml_parser"
|
||||||
version = "1.0.4"
|
version = "1.0.4"
|
||||||
@@ -765,6 +808,7 @@ name = "wfassoc-cdylib"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cbindgen",
|
"cbindgen",
|
||||||
|
"num_enum",
|
||||||
"slotmap",
|
"slotmap",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"wfassoc",
|
"wfassoc",
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
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
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ crate-type = ["cdylib"]
|
|||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
wfassoc = { path="../wfassoc" }
|
wfassoc = { path="../wfassoc" }
|
||||||
slotmap = "1.1.1"
|
slotmap = "1.1.1"
|
||||||
|
num_enum = "0.7.6"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
cbindgen = "0.29.0"
|
cbindgen = "0.29.0"
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ mod cstr_ffi;
|
|||||||
mod last_error;
|
mod last_error;
|
||||||
mod object_pool;
|
mod object_pool;
|
||||||
|
|
||||||
|
use num_enum::TryFromPrimitive;
|
||||||
use object_pool::ObjectPool;
|
use object_pool::ObjectPool;
|
||||||
use std::sync::{LazyLock, RwLock};
|
use std::sync::{LazyLock, RwLock};
|
||||||
use thiserror::Error as TeError;
|
use thiserror::Error as TeError;
|
||||||
@@ -32,6 +33,9 @@ enum Error {
|
|||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
ObjectPool(#[from] object_pool::Error),
|
ObjectPool(#[from] object_pool::Error),
|
||||||
|
|
||||||
|
/// Error occurs when checking enum value
|
||||||
|
#[error("")]
|
||||||
|
EnumOutOfRange,
|
||||||
/// Error when manipulating with poison RwLock
|
/// Error when manipulating with poison RwLock
|
||||||
#[error("RwLock is poisoning")]
|
#[error("RwLock is poisoning")]
|
||||||
PoisonRwLock,
|
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 {
|
macro_rules! pull_reader {
|
||||||
($pool:expr) => {
|
($pool:expr) => {
|
||||||
$pool.read().map_err(|_| Error::PoisonRwLock)
|
$pool.read().map_err(|_| Error::PoisonRwLock)
|
||||||
@@ -173,6 +183,54 @@ macro_rules! cffi_wrapper {
|
|||||||
|
|
||||||
// endregion
|
// 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: Exposed Functions
|
||||||
|
|
||||||
// region: Facilities
|
// region: Facilities
|
||||||
@@ -213,9 +271,6 @@ pub extern "C" fn WFHasPrivilege() -> bool {
|
|||||||
|
|
||||||
// region: Schema
|
// region: Schema
|
||||||
|
|
||||||
static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
|
|
||||||
LazyLock::new(|| RwLock::new(ObjectPool::new()));
|
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
|
pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
|
||||||
cffi_wrapper!(|| -> (out_schema: Token) {
|
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)]
|
#[unsafe(no_mangle)]
|
||||||
pub extern "C" fn WFSchemaSetIdentifier(
|
pub extern "C" fn WFSchemaSetIdentifier(
|
||||||
in_schema: in_param_ty!(Token),
|
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
|
// endregion
|
||||||
|
|
||||||
// region: Program
|
// region: Program
|
||||||
|
|
||||||
static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
|
|
||||||
LazyLock::new(|| RwLock::new(ObjectPool::new()));
|
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub extern "C" fn WFProgramCreate(
|
pub extern "C" fn WFProgramCreate(
|
||||||
in_schema: in_param_ty!(Token),
|
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
|
||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|||||||
@@ -25,34 +25,40 @@ pub struct ObjectPool<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ObjectPool<T> {
|
impl<T> ObjectPool<T> {
|
||||||
|
/// Create a new [ObjectPool].
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
objs: SlotMap::new(),
|
objs: SlotMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert [slotmap] crate's [DefaultKey] to our [Token].
|
||||||
fn key_to_token(key: &DefaultKey) -> Token {
|
fn key_to_token(key: &DefaultKey) -> Token {
|
||||||
key.data().as_ffi()
|
key.data().as_ffi()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert our [Token] to [slotmap] crate's [DefaultKey].
|
||||||
fn token_to_key(token: Token) -> DefaultKey {
|
fn token_to_key(token: Token) -> DefaultKey {
|
||||||
DefaultKey::from(KeyData::from_ffi(token))
|
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> {
|
pub fn allocate(&mut self, value: T) -> Result<Token, Error> {
|
||||||
let key = self.objs.insert(value);
|
let key = self.objs.insert(value);
|
||||||
Ok(Self::key_to_token(&key))
|
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> {
|
pub fn free(&mut self, token: Token) -> Result<(), Error> {
|
||||||
let _ = self.pop(token)?;
|
let _ = self.pop(token)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(&mut self) -> () {
|
/// Remove the object from the pool corresponding to the given token and return it.
|
||||||
self.objs.clear();
|
///
|
||||||
}
|
/// The ownership of the object is transferred to the caller.
|
||||||
|
|
||||||
pub fn pop(&mut self, token: Token) -> Result<T, Error> {
|
pub fn pop(&mut self, token: Token) -> Result<T, Error> {
|
||||||
match self.objs.remove(Self::token_to_key(token)) {
|
match self.objs.remove(Self::token_to_key(token)) {
|
||||||
Some(obj) => Ok(obj),
|
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> {
|
pub fn get(&self, token: Token) -> Result<&T, Error> {
|
||||||
self.objs
|
self.objs
|
||||||
.get(Self::token_to_key(token))
|
.get(Self::token_to_key(token))
|
||||||
.ok_or(Error::NoSuchToken)
|
.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> {
|
pub fn get_mut(&mut self, token: Token) -> Result<&mut T, Error> {
|
||||||
self.objs
|
self.objs
|
||||||
.get_mut(Self::token_to_key(token))
|
.get_mut(Self::token_to_key(token))
|
||||||
|
|||||||
Reference in New Issue
Block a user