aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/stack.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2016-11-05 13:40:53 -0700
committerSteven Fackler <[email protected]>2016-11-05 13:40:53 -0700
commited69d6b0370bd033e78accf6d7a397de25b4ca05 (patch)
tree23a92f59078d472819c659ffa39d9bb03ee1fd93 /openssl/src/stack.rs
parentMore cleanup (diff)
downloadrust-openssl-ed69d6b0370bd033e78accf6d7a397de25b4ca05.tar.xz
rust-openssl-ed69d6b0370bd033e78accf6d7a397de25b4ca05.zip
Add Stack::pop
Diffstat (limited to 'openssl/src/stack.rs')
-rw-r--r--openssl/src/stack.rs25
1 files changed, 13 insertions, 12 deletions
diff --git a/openssl/src/stack.rs b/openssl/src/stack.rs
index 50694919..dae42ca8 100644
--- a/openssl/src/stack.rs
+++ b/openssl/src/stack.rs
@@ -40,18 +40,7 @@ impl<T: Stackable> Stack<T> {
impl<T: Stackable> Drop for Stack<T> {
fn drop(&mut self) {
unsafe {
- loop {
- let ptr = 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 _);
- }
-
+ while let Some(_) = self.pop() {}
OPENSSL_sk_free(self.0 as *mut _);
}
}
@@ -209,6 +198,18 @@ impl<T: Stackable> StackRef<T> {
}
}
+ /// Removes the last element from the stack and returns it.
+ pub fn pop(&mut self) -> Option<T> {
+ unsafe {
+ let ptr = OPENSSL_sk_pop(self.as_stack());
+ if ptr.is_null() {
+ None
+ } else {
+ Some(T::from_ptr(ptr as *mut _))
+ }
+ }
+ }
+
unsafe fn _get(&self, idx: usize) -> *mut T::CType {
OPENSSL_sk_value(self.as_stack(), idx as c_int) as *mut _
}