1use crate::ffi;
2use foreign_types::ForeignTypeRef;
3use libc::c_char;
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;
20}
21
22impl fmt::Display for OpensslString {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 fmt::Display::fmt(&**self, f)
25 }
26}
27
28impl fmt::Debug for OpensslString {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30 fmt::Debug::fmt(&**self, f)
31 }
32}
33
34impl Stackable for OpensslString {
35 type StackType = ffi::stack_st_OPENSSL_STRING;
36}
37
38impl AsRef<str> for OpensslString {
39 fn as_ref(&self) -> &str {
40 self
41 }
42}
43
44impl AsRef<[u8]> for OpensslString {
45 fn as_ref(&self) -> &[u8] {
46 self.as_bytes()
47 }
48}
49
50impl Deref for OpensslStringRef {
51 type Target = str;
52
53 fn deref(&self) -> &str {
54 unsafe {
55 let slice = CStr::from_ptr(self.as_ptr()).to_bytes();
56 str::from_utf8_unchecked(slice)
57 }
58 }
59}
60
61impl AsRef<str> for OpensslStringRef {
62 fn as_ref(&self) -> &str {
63 self
64 }
65}
66
67impl AsRef<[u8]> for OpensslStringRef {
68 fn as_ref(&self) -> &[u8] {
69 self.as_bytes()
70 }
71}
72
73impl fmt::Display for OpensslStringRef {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 fmt::Display::fmt(&**self, f)
76 }
77}
78
79impl fmt::Debug for OpensslStringRef {
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 fmt::Debug::fmt(&**self, f)
82 }
83}
84
85unsafe fn free(buf: *mut c_char) {
86 crate::ffi::OPENSSL_free(buf.cast());
87}