Skip to content

Commit d8fa33b

Browse files
Fix compilation with recent nightlies
The (unstable) `VaList` implementation in std was recently reworked: rust-lang/rust#141980 In particular, `VaListImpl` has been removed (so there's no `as_va_list` method anymore), and `VaList` now has only one lifetime parameter.
1 parent ad00c19 commit d8fa33b

File tree

4 files changed

+19
-32
lines changed

4 files changed

+19
-32
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Now, add your function signature:
7070
use core::ffi::{c_char, c_int};
7171

7272
#[no_mangle]
73-
unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int {
73+
unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int {
7474
todo!()
7575
}
7676
```
@@ -91,7 +91,7 @@ Now, add your logic:
9191
```rust
9292
use printf_compat::{format, output};
9393
let mut s = String::new();
94-
let bytes_written = format(str, args.as_va_list(), output::fmt_write(&mut s));
94+
let bytes_written = format(str, args, output::fmt_write(&mut s));
9595
println!("{}", s);
9696
bytes_written
9797
```

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
//! use core::ffi::{c_char, c_int};
6767
//!
6868
//! #[no_mangle]
69-
//! unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int {
69+
//! unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int {
7070
//! todo!()
7171
//! }
7272
//! ```
@@ -88,10 +88,10 @@
8888
//! # #![feature(c_variadic)]
8989
//! # use core::ffi::{c_char, c_int};
9090
//! # #[no_mangle]
91-
//! # unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int {
91+
//! # unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int {
9292
//! use printf_compat::{format, output};
9393
//! let mut s = String::new();
94-
//! let bytes_written = format(str, args.as_va_list(), output::fmt_write(&mut s));
94+
//! let bytes_written = format(str, args, output::fmt_write(&mut s));
9595
//! println!("{}", s);
9696
//! bytes_written
9797
//! # }

src/output.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,7 @@ pub fn fmt_write(w: &mut impl fmt::Write) -> impl FnMut(Argument) -> c_int + '_
306306
/// # Safety
307307
///
308308
/// [`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/).
309-
pub unsafe fn display<'a, 'b>(
310-
format: *const c_char,
311-
va_list: VaList<'a, 'b>,
312-
) -> VaListDisplay<'a, 'b> {
309+
pub unsafe fn display<'a>(format: *const c_char, va_list: VaList<'a>) -> VaListDisplay<'a> {
313310
VaListDisplay {
314311
format,
315312
va_list,
@@ -327,29 +324,29 @@ pub unsafe fn display<'a, 'b>(
327324
/// use core::ffi::{c_char, c_int};
328325
///
329326
/// #[no_mangle]
330-
/// unsafe extern "C" fn c_library_print(str: *const c_char, mut args: ...) -> c_int {
331-
/// let format = printf_compat::output::display(str, args.as_va_list());
327+
/// unsafe extern "C" fn c_library_print(str: *const c_char, args: ...) -> c_int {
328+
/// let format = printf_compat::output::display(str, args);
332329
/// println!("{}", format);
333330
/// format.bytes_written()
334331
/// }
335332
/// ```
336-
pub struct VaListDisplay<'a, 'b> {
333+
pub struct VaListDisplay<'a> {
337334
format: *const c_char,
338-
va_list: VaList<'a, 'b>,
335+
va_list: VaList<'a>,
339336
written: Cell<c_int>,
340337
}
341338

342-
impl VaListDisplay<'_, '_> {
339+
impl VaListDisplay<'_> {
343340
/// Get the number of bytes written, or 0 if there was an error.
344341
pub fn bytes_written(&self) -> c_int {
345342
self.written.get()
346343
}
347344
}
348345

349-
impl<'a, 'b> fmt::Display for VaListDisplay<'a, 'b> {
346+
impl<'a> fmt::Display for VaListDisplay<'a> {
350347
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
351348
unsafe {
352-
let bytes = crate::format(self.format, self.va_list.clone().as_va_list(), fmt_write(f));
349+
let bytes = crate::format(self.format, self.va_list.clone(), fmt_write(f));
353350
self.written.set(bytes);
354351
if bytes < 0 {
355352
Err(fmt::Error)

tests/tests.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,15 @@ extern "C" {
77
fn free(p: *mut c_void);
88
}
99

10-
unsafe extern "C" fn rust_fmt(str: *const c_char, mut args: ...) -> Box<(c_int, String)> {
10+
unsafe extern "C" fn rust_fmt(str: *const c_char, args: ...) -> Box<(c_int, String)> {
1111
let mut s = String::new();
12-
let bytes_written = printf_compat::format(
13-
str,
14-
args.clone().as_va_list(),
15-
printf_compat::output::fmt_write(&mut s),
16-
);
12+
let bytes_written =
13+
printf_compat::format(str, args.clone(), printf_compat::output::fmt_write(&mut s));
1714
assert!(bytes_written >= 0);
1815
let mut s2 = std::io::Cursor::new(vec![]);
1916
assert_eq!(
2017
bytes_written,
21-
printf_compat::format(
22-
str,
23-
args.as_va_list(),
24-
printf_compat::output::io_write(&mut s2),
25-
)
18+
printf_compat::format(str, args, printf_compat::output::io_write(&mut s2),)
2619
);
2720
assert_eq!(s.as_bytes(), s2.get_ref());
2821
Box::new((bytes_written, s))
@@ -68,11 +61,8 @@ fn assert_fmt_err(fmt: &CStr) {
6861

6962
unsafe extern "C" fn format(str: *const c_char, args: ...) -> c_int {
7063
let mut s = String::new();
71-
let bytes_written = printf_compat::format(
72-
str,
73-
args.clone().as_va_list(),
74-
printf_compat::output::fmt_write(&mut s),
75-
);
64+
let bytes_written =
65+
printf_compat::format(str, args.clone(), printf_compat::output::fmt_write(&mut s));
7666
bytes_written
7767
}
7868
let bytes_written = unsafe { format(fmt.as_ptr()) };

0 commit comments

Comments
 (0)