c_ares/
string.rs

1use std::ops::Deref;
2use std::os::raw::c_char;
3use std::str;
4
5use crate::utils::c_string_as_str_unchecked;
6
7/// A smart pointer wrapping a string as allocated by c-ares.
8pub struct AresString {
9    ares_string: *mut c_char,
10    rust_str: &'static str,
11}
12
13impl AresString {
14    #[allow(dead_code)]
15    pub(crate) fn new(ares_string: *mut c_char) -> Self {
16        let rust_str = unsafe { c_string_as_str_unchecked(ares_string) };
17        AresString {
18            ares_string,
19            rust_str,
20        }
21    }
22}
23
24impl Deref for AresString {
25    type Target = str;
26    fn deref(&self) -> &Self::Target {
27        self.rust_str
28    }
29}
30
31impl Drop for AresString {
32    fn drop(&mut self) {
33        unsafe { c_ares_sys::ares_free_string(self.ares_string.cast()) }
34    }
35}