diff options
Diffstat (limited to 'openssl/src/stack.rs')
| -rw-r--r-- | openssl/src/stack.rs | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/openssl/src/stack.rs b/openssl/src/stack.rs index 372040aa..268afde7 100644 --- a/openssl/src/stack.rs +++ b/openssl/src/stack.rs @@ -5,15 +5,21 @@ use std::convert::AsRef; use std::iter; use std::marker::PhantomData; use std::mem; +use ffi; + +use {cvt, cvt_p}; +use error::ErrorStack; use std::ops::{Deref, DerefMut, Index, IndexMut}; use util::Opaque; #[cfg(ossl10x)] use ffi::{sk_pop as OPENSSL_sk_pop, sk_free as OPENSSL_sk_free, sk_num as OPENSSL_sk_num, - sk_value as OPENSSL_sk_value, _STACK as OPENSSL_STACK}; + sk_value as OPENSSL_sk_value, _STACK as OPENSSL_STACK, + sk_new_null as OPENSSL_sk_new_null, sk_push as OPENSSL_sk_push}; #[cfg(ossl110)] -use ffi::{OPENSSL_sk_pop, OPENSSL_sk_free, OPENSSL_sk_num, OPENSSL_sk_value, OPENSSL_STACK}; +use ffi::{OPENSSL_sk_pop, OPENSSL_sk_free, OPENSSL_sk_num, OPENSSL_sk_value, OPENSSL_STACK, + OPENSSL_sk_new_null, OPENSSL_sk_push}; /// Trait implemented by types which can be placed in a stack. /// @@ -30,9 +36,12 @@ pub trait Stackable: ForeignType { pub struct Stack<T: Stackable>(*mut T::StackType); impl<T: Stackable> Stack<T> { - /// Return a new Stack<T>, taking ownership of the handle - pub unsafe fn from_ptr(stack: *mut T::StackType) -> Stack<T> { - Stack(stack) + pub fn new() -> Result<Stack<T>, ErrorStack> { + unsafe { + ffi::init(); + let ptr = try!(cvt_p(OPENSSL_sk_new_null())); + Ok(Stack(ptr as *mut _)) + } } } @@ -75,9 +84,15 @@ impl<T: Stackable> ForeignType for Stack<T> { type CType = T::StackType; type Ref = StackRef<T>; + #[inline] unsafe fn from_ptr(ptr: *mut T::StackType) -> Stack<T> { Stack(ptr) } + + #[inline] + fn as_ptr(&self) -> *mut T::StackType { + self.0 + } } impl<T: Stackable> Deref for Stack<T> { @@ -197,6 +212,15 @@ impl<T: Stackable> StackRef<T> { } } + /// Pushes a value onto the top of the stack. + pub fn push(&mut self, data: T) -> Result<(), ErrorStack> { + unsafe { + try!(cvt(OPENSSL_sk_push(self.as_stack(), data.as_ptr() as *mut _))); + mem::forget(data); + Ok(()) + } + } + /// Removes the last element from the stack and returns it. pub fn pop(&mut self) -> Option<T> { unsafe { |