• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 use std::{slice, ffi::c_char};
17 use super::request_binding::*;
18 #[repr(C)]
19 pub struct CStringWrapper {
20     c_str: *const c_char,
21     len: u32,
22 }
23 
24 impl CStringWrapper {
from(s: &str) -> Self25     pub fn from(s: &str) -> Self {
26         let c_str = s.as_ptr() as *const c_char;
27         let len = s.len() as u32;
28         CStringWrapper { c_str, len }
29     }
30 
to_string(&self) -> String31     pub fn to_string(&self) -> String {
32         if self.c_str.is_null() || self.len == 0 {
33             unsafe { DeleteChar(self.c_str) };
34             return String::new();
35         }
36         let bytes = unsafe { slice::from_raw_parts(self.c_str as *const u8, self.len as usize) };
37         let str = unsafe { String::from_utf8_unchecked(bytes.to_vec()) };
38         unsafe { DeleteChar(self.c_str) };
39         str
40     }
41 }
42