fix wrap::nix_string_callback!
`cargo test --features exprs` now passes!!
This commit is contained in:
parent
37e78a9052
commit
f834bcfaa9
3 changed files with 18 additions and 26 deletions
|
|
@ -126,7 +126,7 @@ fn test_value_formatting() {
|
|||
assert_eq!(format!("{quoted_str}"), "say \"hello\"");
|
||||
assert_eq!(
|
||||
format!("{quoted_str:?}"),
|
||||
"Value::String(NixString(say \"hello\"))"
|
||||
"Value::String(NixString(\"say \"hello\"\"))"
|
||||
);
|
||||
|
||||
// Test null formatting
|
||||
|
|
@ -141,11 +141,14 @@ fn test_value_formatting() {
|
|||
.eval_from_string("{ a = 1; }", "<eval>")
|
||||
.expect("Failed to evaluate attrs");
|
||||
assert_eq!(format!("{attrs_val}"), "{ <attrs> }");
|
||||
assert_eq!(format!("{attrs_val:?}"), "Value::Attrs({ <attrs> })");
|
||||
assert_eq!(
|
||||
format!("{attrs_val:?}"),
|
||||
"Value::Attrs(NixAttrs({ <attrs> }))"
|
||||
);
|
||||
|
||||
let list_val = state
|
||||
.eval_from_string("[ 1 2 3 ]", "<eval>")
|
||||
.expect("Failed to evaluate list");
|
||||
assert_eq!(format!("{list_val}"), "[ <list> ]");
|
||||
assert_eq!(format!("{list_val:?}"), "Value::List([ <list> ])");
|
||||
assert_eq!(format!("{list_val:?}"), "Value::List(NixList([ <list> ]))");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use std::ffi::{c_char, CStr};
|
||||
use std::ffi::{CStr, c_char};
|
||||
use std::slice::from_raw_parts;
|
||||
use std::str::from_utf8;
|
||||
|
||||
use crate::errors::new_nixide_error;
|
||||
use crate::NixideResult;
|
||||
use crate::errors::new_nixide_error;
|
||||
|
||||
pub trait AsCPtr<T> {
|
||||
#[allow(unused)]
|
||||
|
|
@ -56,7 +56,7 @@ impl CCharPtrExt for *const c_char {
|
|||
if self.is_null() || n == 0 {
|
||||
return Err(new_nixide_error!(NullPtr));
|
||||
}
|
||||
let bytes = unsafe { from_raw_parts(self.cast::<u8>(), n as usize) };
|
||||
let bytes = unsafe { from_raw_parts(self as *const u8, n as usize) };
|
||||
match from_utf8(bytes) {
|
||||
Ok(s) => Ok(s.to_string()),
|
||||
Err(_) => Err(new_nixide_error!(StringNotUtf8)),
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ macro_rules! nonnull {
|
|||
::std::option::Option::Some(p) => ::std::result::Result::Ok(p),
|
||||
::std::option::Option::None => {
|
||||
::std::result::Result::Err($crate::errors::new_nixide_error!(NullPtr))
|
||||
}
|
||||
},
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
|
@ -57,9 +57,7 @@ macro_rules! nix_fn {
|
|||
pub(crate) use nix_fn;
|
||||
|
||||
macro_rules! nix_ptr_fn {
|
||||
($callback:expr $(,)? ) => {{
|
||||
$crate::util::wrap::nix_fn!($callback).and_then(|ptr| $crate::util::wrap::nonnull!(ptr))
|
||||
}};
|
||||
($callback:expr $(,)? ) => {{ $crate::util::wrap::nix_fn!($callback).and_then(|ptr| $crate::util::wrap::nonnull!(ptr)) }};
|
||||
}
|
||||
pub(crate) use nix_ptr_fn;
|
||||
|
||||
|
|
@ -114,34 +112,25 @@ pub(crate) use nix_callback;
|
|||
|
||||
macro_rules! nix_string_callback {
|
||||
($function:expr $(,)?) => {{
|
||||
#[repr(C)]
|
||||
struct __ReturnType {
|
||||
start: *const ::std::ffi::c_char,
|
||||
n: ::std::ffi::c_uint,
|
||||
}
|
||||
|
||||
let __result = $crate::util::wrap::nix_callback!(
|
||||
|start: *const ::std::ffi::c_char, n: ::std::ffi::c_uint; userdata: ();| -> (*const ::std::ffi::c_char, ::std::ffi::c_uint) {
|
||||
|start: *const ::std::ffi::c_char, n: ::std::ffi::c_uint; userdata: ();| -> $crate::NixideResult<String> {
|
||||
unsafe {
|
||||
let retval = &raw mut (*userdata).retval;
|
||||
retval.write((start, n))
|
||||
retval.write($crate::stdext::CCharPtrExt::to_utf8_string_n(start, n as usize))
|
||||
}
|
||||
},
|
||||
$function
|
||||
);
|
||||
|
||||
__result.and_then(|(start, n)| {
|
||||
let __return = $crate::stdext::CCharPtrExt::to_utf8_string_n(start, n as usize);
|
||||
|
||||
__return
|
||||
})
|
||||
match __result {
|
||||
Ok(res) => res,
|
||||
Err(res) => Err(res),
|
||||
}
|
||||
}};
|
||||
}
|
||||
pub(crate) use nix_string_callback;
|
||||
|
||||
macro_rules! nix_pathbuf_callback {
|
||||
($function:expr $(,)?) => {{
|
||||
$crate::util::wrap::nix_string_callback!($function).map(::std::path::PathBuf::from)
|
||||
}};
|
||||
($function:expr $(,)?) => {{ $crate::util::wrap::nix_string_callback!($function).map(::std::path::PathBuf::from) }};
|
||||
}
|
||||
pub(crate) use nix_pathbuf_callback;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue