2018-08-30 08:57:17 -05:00
|
|
|
//ignore-windows: Uses POSIX APIs
|
2018-12-13 13:25:24 -06:00
|
|
|
#![feature(rustc_private)]
|
2017-06-21 07:06:23 -05:00
|
|
|
|
|
|
|
extern crate libc;
|
2018-11-20 07:11:27 -06:00
|
|
|
|
2017-06-21 07:06:23 -05:00
|
|
|
use std::ffi::CString;
|
|
|
|
|
|
|
|
mod mlibc {
|
|
|
|
use libc::{c_char, size_t};
|
|
|
|
extern {
|
|
|
|
#[link_name = "strlen"]
|
|
|
|
pub fn my_strlen(str: *const c_char) -> size_t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn strlen(str: String) -> usize {
|
|
|
|
// C string is terminated with a zero
|
|
|
|
let s = CString::new(str).unwrap();
|
|
|
|
unsafe {
|
|
|
|
mlibc::my_strlen(s.as_ptr()) as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let len = strlen("Rust".to_string());
|
|
|
|
assert_eq!(len, 4);
|
|
|
|
}
|