rust/tests/run-pass/foreign-fn-linkname.rs

28 lines
528 B
Rust
Raw Normal View History

//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;
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);
}