1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#![allow(non_snake_case)] #![allow(non_camel_case_types)] use std::{ os::raw::*, slice, str }; #[repr(C)] #[derive(Copy)] pub struct bw_CStrSlice { pub len: usize, pub data: *const c_char } extern "C" { pub fn bw_string_freeCstr( str: *const c_char ); } impl Clone for bw_CStrSlice { fn clone( &self ) -> Self { panic!("bw_CStrSlice is not actually supposed to be Clone!"); } } impl From<&str> for bw_CStrSlice { fn from( string: &str ) -> Self { Self { data: string.as_bytes().as_ptr() as *const c_char, len: string.len() } } } impl<'a> Into<&'a str> for bw_CStrSlice { fn into( self ) -> &'a str { let raw: &[u8] = unsafe { slice::from_raw_parts(self.data as _, self.len ) }; #[cfg(debug_assertions)] return str::from_utf8( raw ).expect("Invalid UTF-8"); #[cfg(not(debug_assertions))] return unsafe { str::from_utf8_unchecked( raw ) }; } } impl Into<String> for bw_CStrSlice { fn into( self ) -> String { let str: &str = self.into(); str.to_owned() } }