From 01ae978db0dc8620b2cc754c0d5cf94a68c1f549 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 4 Nov 2016 16:32:20 -0700 Subject: Get rid of Ref There's unfortunately a rustdoc bug that causes all methods implemented for any Ref to be inlined in the deref methods section :( --- openssl/src/stack.rs | 82 ++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 38 deletions(-) (limited to 'openssl/src/stack.rs') diff --git a/openssl/src/stack.rs b/openssl/src/stack.rs index 368b6e07..ad208377 100644 --- a/openssl/src/stack.rs +++ b/openssl/src/stack.rs @@ -2,10 +2,12 @@ use std::ops::{Deref, DerefMut, Index, IndexMut}; use std::iter; use std::borrow::Borrow; use std::convert::AsRef; +use std::marker::PhantomData; use libc::c_int; use ffi; -use types::{OpenSslType, Ref}; +use types::{OpenSslType, OpenSslTypeRef}; +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, @@ -55,45 +57,49 @@ impl Drop for Stack { } } -impl AsRef>> for Stack { - fn as_ref(&self) -> &Ref> { +impl AsRef> for Stack { + fn as_ref(&self) -> &StackRef { &*self } } -impl Borrow>> for Stack { - fn borrow(&self) -> &Ref> { +impl Borrow> for Stack { + fn borrow(&self) -> &StackRef { &*self } } -unsafe impl OpenSslType for Stack { +impl OpenSslType for Stack { type CType = T::StackType; + type Ref = StackRef; unsafe fn from_ptr(ptr: *mut T::StackType) -> Stack { Stack(ptr) } - - fn as_ptr(&self) -> *mut T::StackType { - self.0 - } } impl Deref for Stack { - type Target = Ref>; + type Target = StackRef; - fn deref(&self) -> &Ref> { - unsafe { Ref::from_ptr(self.0) } + fn deref(&self) -> &StackRef { + unsafe { StackRef::from_ptr(self.0) } } } impl DerefMut for Stack { - fn deref_mut(&mut self) -> &mut ::types::Ref> { - unsafe { Ref::from_ptr_mut(self.0) } + fn deref_mut(&mut self) -> &mut StackRef { + unsafe { StackRef::from_ptr_mut(self.0) } } } -impl Ref> { +pub struct StackRef(Opaque, PhantomData); + +impl OpenSslTypeRef for StackRef { + type CType = T::StackType; +} + + +impl StackRef { /// OpenSSL stack types are just a (kinda) typesafe wrapper around /// a `_STACK` object. We can therefore safely cast it and access /// the `_STACK` members without having to worry about the real @@ -141,25 +147,25 @@ impl Ref> { /// Returns a reference to the element at the given index in the /// stack or `None` if the index is out of bounds - pub fn get(&self, idx: usize) -> Option<&Ref> { + pub fn get(&self, idx: usize) -> Option<&T::Ref> { unsafe { if idx >= self.len() { return None; } - Some(Ref::from_ptr(self._get(idx))) + Some(T::Ref::from_ptr(self._get(idx))) } } /// Returns a mutable reference to the element at the given index in the /// stack or `None` if the index is out of bounds - pub fn get_mut(&mut self, idx: usize) -> Option<&mut Ref> { + pub fn get_mut(&mut self, idx: usize) -> Option<&mut T::Ref> { unsafe { if idx >= self.len() { return None; } - Some(Ref::from_ptr_mut(self._get(idx))) + Some(T::Ref::from_ptr_mut(self._get(idx))) } } @@ -168,22 +174,22 @@ impl Ref> { } } -impl Index for Ref> { - type Output = Ref; +impl Index for StackRef { + type Output = T::Ref; - fn index(&self, index: usize) -> &Ref { + fn index(&self, index: usize) -> &T::Ref { self.get(index).unwrap() } } -impl IndexMut for Ref> { - fn index_mut(&mut self, index: usize) -> &mut Ref { +impl IndexMut for StackRef { + fn index_mut(&mut self, index: usize) -> &mut T::Ref { self.get_mut(index).unwrap() } } -impl<'a, T: Stackable> iter::IntoIterator for &'a Ref> { - type Item = &'a Ref; +impl<'a, T: Stackable> iter::IntoIterator for &'a StackRef { + type Item = &'a T::Ref; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { @@ -191,8 +197,8 @@ impl<'a, T: Stackable> iter::IntoIterator for &'a Ref> { } } -impl<'a, T: Stackable> iter::IntoIterator for &'a mut Ref> { - type Item = &'a mut Ref; +impl<'a, T: Stackable> iter::IntoIterator for &'a mut StackRef { + type Item = &'a mut T::Ref; type IntoIter = IterMut<'a, T>; fn into_iter(self) -> IterMut<'a, T> { @@ -201,7 +207,7 @@ impl<'a, T: Stackable> iter::IntoIterator for &'a mut Ref> { } impl<'a, T: Stackable> iter::IntoIterator for &'a Stack { - type Item = &'a Ref; + type Item = &'a T::Ref; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { @@ -210,7 +216,7 @@ impl<'a, T: Stackable> iter::IntoIterator for &'a Stack { } impl<'a, T: Stackable> iter::IntoIterator for &'a mut Stack { - type Item = &'a mut Ref; + type Item = &'a mut T::Ref; type IntoIter = IterMut<'a, T>; fn into_iter(self) -> IterMut<'a, T> { @@ -221,14 +227,14 @@ impl<'a, T: Stackable> iter::IntoIterator for &'a mut Stack { /// An iterator over the stack's contents. pub struct Iter<'a, T: Stackable> where T: 'a { - stack: &'a Ref>, + stack: &'a StackRef, pos: usize, } impl<'a, T: Stackable> iter::Iterator for Iter<'a, T> { - type Item = &'a Ref; + type Item = &'a T::Ref; - fn next(&mut self) -> Option<&'a Ref> { + fn next(&mut self) -> Option<&'a T::Ref> { let n = self.stack.get(self.pos); if n.is_some() { @@ -250,14 +256,14 @@ impl<'a, T: Stackable> iter::ExactSizeIterator for Iter<'a, T> { /// A mutable iterator over the stack's contents. pub struct IterMut<'a, T: Stackable + 'a> { - stack: &'a mut Ref>, + stack: &'a mut StackRef, pos: usize, } impl<'a, T: Stackable> iter::Iterator for IterMut<'a, T> { - type Item = &'a mut Ref; + type Item = &'a mut T::Ref; - fn next(&mut self) -> Option<&'a mut Ref> { + fn next(&mut self) -> Option<&'a mut T::Ref> { if self.pos >= self.stack.len() { None } else { @@ -267,7 +273,7 @@ impl<'a, T: Stackable> iter::Iterator for IterMut<'a, T> { // the same object, so we have to use unsafe code for // mutable iterators. let n = unsafe { - Some(Ref::from_ptr_mut(self.stack._get(self.pos))) + Some(T::Ref::from_ptr_mut(self.stack._get(self.pos))) }; self.pos += 1; -- cgit v1.2.3