diff options
| author | Steven Fackler <[email protected]> | 2016-10-26 21:28:00 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-10-26 21:28:00 -0700 |
| commit | 4f59d576752aa1f44704b19befbf893dbd046465 (patch) | |
| tree | c93bb43c69bda0713140fe788680c333daa841a9 /openssl/src/crypto.rs | |
| parent | Add SslMethod::from_ptr (diff) | |
| download | rust-openssl-4f59d576752aa1f44704b19befbf893dbd046465.tar.xz rust-openssl-4f59d576752aa1f44704b19befbf893dbd046465.zip | |
Move SslString to a shared location
Diffstat (limited to 'openssl/src/crypto.rs')
| -rw-r--r-- | openssl/src/crypto.rs | 43 |
1 files changed, 43 insertions, 0 deletions
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) + } +} |