From 4f59d576752aa1f44704b19befbf893dbd046465 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 26 Oct 2016 21:28:00 -0700 Subject: Move SslString to a shared location --- openssl/src/crypto.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 openssl/src/crypto.rs (limited to 'openssl/src/crypto.rs') diff --git a/openssl/src/crypto.rs b/openssl/src/crypto.rs new file mode 100644 index 00000000..0a121fa0 --- /dev/null +++ b/openssl/src/crypto.rs @@ -0,0 +1,43 @@ +use ffi; +use libc::{c_int, c_void}; +use std::fmt; +use std::slice; +use std::ops::Deref; +use std::str; + +pub struct CryptoString(&'static str); + +impl<'s> Drop for CryptoString { + fn drop(&mut self) { + unsafe { + CRYPTO_free!(self.0.as_ptr() as *mut c_void); + } + } +} + +impl Deref for CryptoString { + type Target = str; + + fn deref(&self) -> &str { + self.0 + } +} + +impl CryptoString { + pub unsafe fn from_raw_parts(buf: *const u8, len: usize) -> CryptoString { + let slice = slice::from_raw_parts(buf, len); + CryptoString(str::from_utf8_unchecked(slice)) + } +} + +impl fmt::Display for CryptoString { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self.0, f) + } +} + +impl fmt::Debug for CryptoString { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(self.0, f) + } +} -- cgit v1.2.3