browser_window_ffi/
string.rs

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