This commit is an implementation of [RFC 494][rfc] which removes the entire `std::c_vec` module and redesigns the `std::c_str` module as `std::ffi`. [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0494-c_str-and-c_vec-stability.md The interface of the new `CString` is outlined in the linked RFC, the primary changes being: * The `ToCStr` trait is gone, meaning the `with_c_str` and `to_c_str` methods are now gone. These two methods are replaced with a `CString::from_slice` method. * The `CString` type is now just a wrapper around `Vec<u8>` with a static guarantee that there is a trailing nul byte with no internal nul bytes. This means that `CString` now implements `Deref<Target = [c_char]>`, which is where it gains most of its methods from. A few helper methods are added to acquire a slice of `u8` instead of `c_char`, as well as including a slice with the trailing nul byte if necessary. * All usage of non-owned `CString` values is now done via two functions inside of `std::ffi`, called `c_str_to_bytes` and `c_str_to_bytes_with_nul`. These functions are now the one method used to convert a `*const c_char` to a Rust slice of `u8`. Many more details, including newly deprecated methods, can be found linked in the RFC. This is a: [breaking-change] Closes #20444
64 lines
2.1 KiB
Rust
64 lines
2.1 KiB
Rust
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
extern crate libc;
|
|
|
|
use std::ffi::{self, CString};
|
|
use libc::{c_char, c_int};
|
|
|
|
// ignore-fast doesn't like extern crate
|
|
|
|
extern {
|
|
fn sprintf(s: *mut c_char, format: *const c_char, ...) -> c_int;
|
|
}
|
|
|
|
unsafe fn check<T, F>(expected: &str, f: F) where F: FnOnce(*mut c_char) -> T {
|
|
let mut x = [0 as c_char; 50];
|
|
f(&mut x[0] as *mut c_char);
|
|
assert_eq!(expected.as_bytes(), ffi::c_str_to_bytes(&x.as_ptr()));
|
|
}
|
|
|
|
pub fn main() {
|
|
|
|
unsafe {
|
|
// Call with just the named parameter
|
|
let c = CString::from_slice(b"Hello World\n");
|
|
check("Hello World\n", |s| sprintf(s, c.as_ptr()));
|
|
|
|
// Call with variable number of arguments
|
|
let c = CString::from_slice(b"%d %f %c %s\n");
|
|
check("42 42.500000 a %d %f %c %s\n\n", |s| {
|
|
sprintf(s, c.as_ptr(), 42i, 42.5f64, 'a' as c_int, c.as_ptr());
|
|
});
|
|
|
|
// Make a function pointer
|
|
let x: unsafe extern fn(*mut c_char, *const c_char, ...) -> c_int = sprintf;
|
|
|
|
// A function that takes a function pointer
|
|
unsafe fn call(p: unsafe extern fn(*mut c_char, *const c_char, ...) -> c_int) {
|
|
// Call with just the named parameter
|
|
let c = CString::from_slice(b"Hello World\n");
|
|
check("Hello World\n", |s| sprintf(s, c.as_ptr()));
|
|
|
|
// Call with variable number of arguments
|
|
let c = CString::from_slice(b"%d %f %c %s\n");
|
|
check("42 42.500000 a %d %f %c %s\n\n", |s| {
|
|
sprintf(s, c.as_ptr(), 42i, 42.5f64, 'a' as c_int, c.as_ptr());
|
|
});
|
|
}
|
|
|
|
// Pass sprintf directly
|
|
call(sprintf);
|
|
|
|
// Pass sprintf indirectly
|
|
call(x);
|
|
}
|
|
|
|
}
|