diff --git a/README.md b/README.md index ed2b57e..d64f149 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Now, add your function signature: use core::ffi::{c_char, c_int}; #[no_mangle] -unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int { +unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int { todo!() } ``` @@ -91,7 +91,7 @@ Now, add your logic: ```rust use printf_compat::{format, output}; let mut s = String::new(); -let bytes_written = format(str, args.as_va_list(), output::fmt_write(&mut s)); +let bytes_written = format(str, args, output::fmt_write(&mut s)); println!("{}", s); bytes_written ``` diff --git a/src/lib.rs b/src/lib.rs index e99335d..e1c6abc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,7 +66,7 @@ //! use core::ffi::{c_char, c_int}; //! //! #[no_mangle] -//! unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int { +//! unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int { //! todo!() //! } //! ``` @@ -88,10 +88,10 @@ //! # #![feature(c_variadic)] //! # use core::ffi::{c_char, c_int}; //! # #[no_mangle] -//! # unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int { +//! # unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int { //! use printf_compat::{format, output}; //! let mut s = String::new(); -//! let bytes_written = format(str, args.as_va_list(), output::fmt_write(&mut s)); +//! let bytes_written = format(str, args, output::fmt_write(&mut s)); //! println!("{}", s); //! bytes_written //! # } diff --git a/src/output.rs b/src/output.rs index 3853935..0367ff8 100644 --- a/src/output.rs +++ b/src/output.rs @@ -306,10 +306,7 @@ pub fn fmt_write(w: &mut impl fmt::Write) -> impl FnMut(Argument) -> c_int + '_ /// # Safety /// /// [`VaList`]s are *very* unsafe. The passed `format` and `args` parameter must be a valid [`printf` format string](http://www.cplusplus.com/reference/cstdio/printf/). -pub unsafe fn display<'a, 'b>( - format: *const c_char, - va_list: VaList<'a, 'b>, -) -> VaListDisplay<'a, 'b> { +pub unsafe fn display<'a>(format: *const c_char, va_list: VaList<'a>) -> VaListDisplay<'a> { VaListDisplay { format, va_list, @@ -327,29 +324,29 @@ pub unsafe fn display<'a, 'b>( /// use core::ffi::{c_char, c_int}; /// /// #[no_mangle] -/// unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int { -/// let format = printf_compat::output::display(str, args.as_va_list()); +/// unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int { +/// let format = printf_compat::output::display(str, args); /// println!("{}", format); /// format.bytes_written() /// } /// ``` -pub struct VaListDisplay<'a, 'b> { +pub struct VaListDisplay<'a> { format: *const c_char, - va_list: VaList<'a, 'b>, + va_list: VaList<'a>, written: Cell, } -impl VaListDisplay<'_, '_> { +impl VaListDisplay<'_> { /// Get the number of bytes written, or 0 if there was an error. pub fn bytes_written(&self) -> c_int { self.written.get() } } -impl<'a, 'b> fmt::Display for VaListDisplay<'a, 'b> { +impl<'a> fmt::Display for VaListDisplay<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { unsafe { - let bytes = crate::format(self.format, self.va_list.clone().as_va_list(), fmt_write(f)); + let bytes = crate::format(self.format, self.va_list.clone(), fmt_write(f)); self.written.set(bytes); if bytes < 0 { Err(fmt::Error) diff --git a/tests/tests.rs b/tests/tests.rs index 74a7903..48ab90f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -7,22 +7,15 @@ extern "C" { fn free(p: *mut c_void); } -unsafe extern "C" fn rust_fmt(str: *const c_char, mut args: ...) -> Box<(c_int, String)> { +unsafe extern "C" fn rust_fmt(str: *const c_char, args: ...) -> Box<(c_int, String)> { let mut s = String::new(); - let bytes_written = printf_compat::format( - str, - args.clone().as_va_list(), - printf_compat::output::fmt_write(&mut s), - ); + let bytes_written = + printf_compat::format(str, args.clone(), printf_compat::output::fmt_write(&mut s)); assert!(bytes_written >= 0); let mut s2 = std::io::Cursor::new(vec![]); assert_eq!( bytes_written, - printf_compat::format( - str, - args.as_va_list(), - printf_compat::output::io_write(&mut s2), - ) + printf_compat::format(str, args, printf_compat::output::io_write(&mut s2),) ); assert_eq!(s.as_bytes(), s2.get_ref()); Box::new((bytes_written, s)) @@ -68,11 +61,8 @@ fn assert_fmt_err(fmt: &CStr) { unsafe extern "C" fn format(str: *const c_char, args: ...) -> c_int { let mut s = String::new(); - let bytes_written = printf_compat::format( - str, - args.clone().as_va_list(), - printf_compat::output::fmt_write(&mut s), - ); + let bytes_written = + printf_compat::format(str, args.clone(), printf_compat::output::fmt_write(&mut s)); bytes_written } let bytes_written = unsafe { format(fmt.as_ptr()) };