upgrade magic-sys to 0.4.2 and add get/set param functions#433
upgrade magic-sys to 0.4.2 and add get/set param functions#433rainmote wants to merge 2 commits into
Conversation
Signed-off-by: rainmote <rainmote@gmail.com>
Signed-off-by: rainmote <rainmote@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #433 +/- ##
==========================================
+ Coverage 47.86% 49.01% +1.15%
==========================================
Files 2 2
Lines 491 608 +117
Branches 491 608 +117
==========================================
+ Hits 235 298 +63
- Misses 255 309 +54
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks for the merge request, params are a milestone to get However there's some changes I'd like to see: The Roughly this: enum ParamKind {
IndirMax,
}
enum Param {
IndirMax(c_size_t),
}
fn get_param(&self, param: ParamKind) -> Result<Param, GetParamError> {}
fn set_param(&self, param: &Param) -> Result<(), SetParamError> {}The suggested code uses There's some smaller things like documentation about params not being updated elsewhere, bumping the toolchain version without MSRV, I like the consistency with other code and docs and tests! There might be other changes that I didn't think of yet How do you want to proceed? |
|
On second thought the enum is unergonomic because users have to match on it unsafe trait Param {
fn kind() -> c_int;
fn uninit() -> Self;
unsafe fn as_mut_c_void(&mut self) -> *mut c_void;
unsafe fn as_c_void(&self) -> *const c_void;
}
pub struct IndirMax(pub c_size_t)
impl Param for IndirMax {
fn kind() -> c_int {
MAGIC_PARAM_INDIR_MAX
}
fn uninit() -> Self {
Self(0)
}
fn as_mut_c_void(&mut self) -> *mut c_void {
self.0 as *mut c_size_t as *mut _
}
fn as_c_void(&self) -> *const c_void {
self.0 as *const c_size_t as *const _
}
}
fn get_param<P: Param>(&self) -> Result<P, GetParamError> {
let mut param = P::uninit();
unsafe { magic_getparam(P::kind(), param.as_mut_c_void()); }
Ok(param)
}
fn set_param<P: Param>(&self, param: &P) -> Result<(), SetParamError> {
unsafe { magic_setparam(P::kind(), param.as_c_void()); }
Ok(())
}
let indir_max = cookie.get_param::<IndirMax>()?;
cookie.set_param(&IndirMax(42))?;
More elaborate would be |
|
Behold my latest design idea type ParamKind = c_int;
/// # Safety
///
/// `Self::Value` must match the type `libmagic` is expecting for `Self::KIND`
unsafe trait Param {
type Value;
const KIND: ParamKind;
fn from_value(value: Self::Value) -> Self;
fn as_ptr(&self) -> *const Self::Value;
}
pub struct IndirMax(pub c_size_t);
unsafe impl Param for IndirMax {
type Value = c_size_t;
const KIND: ParamKind = MAGIC_PARAM_INDIR_MAX;
fn from_value(value: Self::Value) -> Self {
Self(value)
}
fn as_ptr(&self) -> *const Self::Value {
&self.0 as *const _
}
}
impl Cookie {
pub fn get_param<P: Param>(&self) -> Result<P, GetParamError> {
let mut value = MaybeUninit::uninit();
let ptr = value.as_mut_ptr() as *mut _;
unsafe {
magic_getparam(P::KIND, ptr);
}
let value = unsafe { value.assume_init() };
let param = P::from_value(value);
Ok(param)
}
pub fn set_param<P: Param>(&self, param: &P) -> Result<(), SetParamError> {
let ptr = param.as_ptr() as *const _;
unsafe {
magic_setparam(P::KIND, ptr);
}
Ok(())
}
}
let indir_max = cookie.get_param::<IndirMax>()?;
cookie.set_param(&IndirMax(42))?; |
|
Hej @rainmote I intend to merge #453 soon, and close your merge request without merging it |
References
Description