boring_imp/
string.rs

1use crate::ffi;
2use foreign_types::ForeignTypeRef;
3use libc::{c_char, c_void};
4use std::convert::AsRef;
5use std::ffi::CStr;
6use std::fmt;
7use std::ops::Deref;
8use std::str;
9
10use crate::stack::Stackable;
11
12foreign_type_and_impl_send_sync! {
13    type CType = c_char;
14    fn drop = free;
15
16    pub struct OpensslString;
17}
18
19impl fmt::Display for OpensslString {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        fmt::Display::fmt(&**self, f)
22    }
23}
24
25impl fmt::Debug for OpensslString {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        fmt::Debug::fmt(&**self, f)
28    }
29}
30
31impl Stackable for OpensslString {
32    type StackType = ffi::stack_st_OPENSSL_STRING;
33}
34
35impl AsRef<str> for OpensslString {
36    fn as_ref(&self) -> &str {
37        self
38    }
39}
40
41impl AsRef<[u8]> for OpensslString {
42    fn as_ref(&self) -> &[u8] {
43        self.as_bytes()
44    }
45}
46
47impl Deref for OpensslStringRef {
48    type Target = str;
49
50    fn deref(&self) -> &str {
51        unsafe {
52            let slice = CStr::from_ptr(self.as_ptr()).to_bytes();
53            str::from_utf8_unchecked(slice)
54        }
55    }
56}
57
58impl AsRef<str> for OpensslStringRef {
59    fn as_ref(&self) -> &str {
60        self
61    }
62}
63
64impl AsRef<[u8]> for OpensslStringRef {
65    fn as_ref(&self) -> &[u8] {
66        self.as_bytes()
67    }
68}
69
70impl fmt::Display for OpensslStringRef {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        fmt::Display::fmt(&**self, f)
73    }
74}
75
76impl fmt::Debug for OpensslStringRef {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        fmt::Debug::fmt(&**self, f)
79    }
80}
81
82unsafe fn free(buf: *mut c_char) {
83    crate::ffi::OPENSSL_free(buf as *mut c_void);
84}