]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
abe05a73 | 2 | // ignore-wasm32-bare no libc to test ffi with |
48663c56 | 3 | // ignore-sgx no libc |
c34b1796 | 4 | |
0731742a | 5 | #![feature(rustc_private)] |
970d7e83 | 6 | |
1a4d82fc JJ |
7 | extern crate libc; |
8 | use std::ffi::CString; | |
223e47cc | 9 | |
1a4d82fc JJ |
10 | mod mlibc { |
11 | use libc::{c_char, size_t}; | |
12 | ||
5869c6ff | 13 | extern "C" { |
223e47cc | 14 | #[link_name = "strlen"] |
1a4d82fc | 15 | pub fn my_strlen(str: *const c_char) -> size_t; |
223e47cc LB |
16 | } |
17 | } | |
18 | ||
c34b1796 | 19 | fn strlen(str: String) -> usize { |
1a4d82fc | 20 | // C string is terminated with a zero |
85aaf69f | 21 | let s = CString::new(str).unwrap(); |
5869c6ff | 22 | unsafe { mlibc::my_strlen(s.as_ptr()) as usize } |
223e47cc LB |
23 | } |
24 | ||
25 | pub fn main() { | |
1a4d82fc | 26 | let len = strlen("Rust".to_string()); |
c34b1796 | 27 | assert_eq!(len, 4); |
223e47cc | 28 | } |