diff --git a/nixide/src/expr/tests.rs b/nixide/src/expr/tests.rs index 9d2df97..0b32729 100644 --- a/nixide/src/expr/tests.rs +++ b/nixide/src/expr/tests.rs @@ -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; }", "") .expect("Failed to evaluate attrs"); assert_eq!(format!("{attrs_val}"), "{ }"); - assert_eq!(format!("{attrs_val:?}"), "Value::Attrs({ })"); + assert_eq!( + format!("{attrs_val:?}"), + "Value::Attrs(NixAttrs({ }))" + ); let list_val = state .eval_from_string("[ 1 2 3 ]", "") .expect("Failed to evaluate list"); assert_eq!(format!("{list_val}"), "[ ]"); - assert_eq!(format!("{list_val:?}"), "Value::List([ ])"); + assert_eq!(format!("{list_val:?}"), "Value::List(NixList([ ]))"); } diff --git a/nixide/src/stdext/cchar_ptr.rs b/nixide/src/stdext/cchar_ptr.rs index 0a91b86..bf1840e 100644 --- a/nixide/src/stdext/cchar_ptr.rs +++ b/nixide/src/stdext/cchar_ptr.rs @@ -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 { #[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::(), 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)), diff --git a/nixide/src/util/wrap.rs b/nixide/src/util/wrap.rs index 33790c9..5b9898e 100644 --- a/nixide/src/util/wrap.rs +++ b/nixide/src/util/wrap.rs @@ -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 { 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;