diff options
| author | Steven Fackler <[email protected]> | 2016-10-26 21:42:09 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-10-26 21:42:09 -0700 |
| commit | 654f0941e18376d37c9e0f97d28f44f6b16cd2b5 (patch) | |
| tree | 0812f41edba34706abff586f3aa182ed1b90b919 /openssl/src/crypto.rs | |
| parent | Move SslString to a shared location (diff) | |
| download | rust-openssl-654f0941e18376d37c9e0f97d28f44f6b16cd2b5.tar.xz rust-openssl-654f0941e18376d37c9e0f97d28f44f6b16cd2b5.zip | |
Don't double-allocate strings
Diffstat (limited to 'openssl/src/crypto.rs')
| -rw-r--r-- | openssl/src/crypto.rs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/openssl/src/crypto.rs b/openssl/src/crypto.rs index 0a121fa0..26f66d0e 100644 --- a/openssl/src/crypto.rs +++ b/openssl/src/crypto.rs @@ -1,6 +1,6 @@ -use ffi; -use libc::{c_int, c_void}; +use libc::{c_char, c_void}; use std::fmt; +use std::ffi::CStr; use std::slice; use std::ops::Deref; use std::str; @@ -24,10 +24,15 @@ impl Deref for CryptoString { } impl CryptoString { - pub unsafe fn from_raw_parts(buf: *const u8, len: usize) -> CryptoString { + pub unsafe fn from_raw_parts(buf: *mut u8, len: usize) -> CryptoString { let slice = slice::from_raw_parts(buf, len); CryptoString(str::from_utf8_unchecked(slice)) } + + pub unsafe fn from_null_terminated(buf: *mut c_char) -> CryptoString { + let slice = CStr::from_ptr(buf).to_bytes(); + CryptoString(str::from_utf8_unchecked(slice)) + } } impl fmt::Display for CryptoString { |