From 79c51d5e517ffe40e01c34577667c0a242a5f6ba Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 1 Nov 2016 19:12:38 -0700 Subject: [PATCH] Clean up stack destructor --- openssl/src/stack.rs | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/openssl/src/stack.rs b/openssl/src/stack.rs index 2cc53b5f..bb1ddfda 100644 --- a/openssl/src/stack.rs +++ b/openssl/src/stack.rs @@ -9,12 +9,19 @@ use libc::c_int; use ffi; use types::{OpenSslType, Ref}; -/// Trait implemented by stackable types. This must *only* be -/// implemented on opaque types that can be directly casted into their -/// `CType`. +#[cfg(ossl10x)] +use ffi::{sk_pop as OPENSSL_sk_pop, sk_free as OPENSSL_sk_free}; +#[cfg(ossl110)] +use ffi::{OPENSSL_sk_pop, OPENSSL_sk_free}; + +/// Trait implemented by types which can be placed in a stack. +/// +/// Like `OpenSslType`, it should not be implemented for any type outside +/// of this crate. pub trait Stackable: OpenSslType { - /// C stack type for this element. Generally called - /// `stack_st_{ELEMENT_TYPE}`, normally hidden by the + /// The C stack type for this element. + /// + /// Generally called `stack_st_{ELEMENT_TYPE}`, normally hidden by the /// `STACK_OF(ELEMENT_TYPE)` macro in the OpenSSL API. type StackType; } @@ -30,11 +37,10 @@ impl Stack { } impl Drop for Stack { - #[cfg(ossl10x)] fn drop(&mut self) { unsafe { loop { - let ptr = ffi::sk_pop(self.as_stack()); + let ptr = OPENSSL_sk_pop(self.as_stack()); if ptr.is_null() { break; @@ -45,26 +51,7 @@ impl Drop for Stack { T::from_ptr(ptr as *mut _); } - ffi::sk_free(self.0 as *mut _); - } - } - - #[cfg(ossl110)] - fn drop(&mut self) { - unsafe { - loop { - let ptr = ffi::OPENSSL_sk_pop(self.as_stack()); - - if ptr.is_null() { - break; - } - - // Build the owned version of the object just to run - // its `drop` implementation and delete the item. - T::from_ptr(ptr as *mut _); - } - - ffi::OPENSSL_sk_free(self.0 as *mut _); + OPENSSL_sk_free(self.0 as *mut _); } } }