Skip to content

Commit

Permalink
Merge #186
Browse files Browse the repository at this point in the history
186: Remove bounds on establish, test that Send gets derived as needed r=jethrogb a=AdrianCX



Co-authored-by: Adrian Cruceru <[email protected]>
  • Loading branch information
bors[bot] and AdrianCX authored Feb 11, 2022
2 parents 51ad965 + 1e096e7 commit 020e01d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
62 changes: 58 additions & 4 deletions mbedtls/src/ssl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait IoCallback {
fn data_ptr(&mut self) -> *mut c_void;
}

impl<IO: Read + Write + 'static> IoCallback for IO {
impl<IO: Read + Write> IoCallback for IO {
unsafe extern "C" fn call_recv(user_data: *mut c_void, data: *mut c_uchar, len: size_t) -> c_int {
let len = if len > (c_int::max_value() as size_t) {
c_int::max_value() as size_t
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<T> Context<T> {
}
}

impl<T: IoCallback + Send + Sync + 'static> Context<T> {
impl<T: IoCallback> Context<T> {
pub fn establish(&mut self, io: T, hostname: Option<&str>) -> Result<()> {
unsafe {
let mut io = Box::new(io);
Expand Down Expand Up @@ -412,13 +412,67 @@ impl HandshakeContext {

#[cfg(test)]
mod tests {
use crate::ssl::context::HandshakeContext;
use crate::ssl::context::{HandshakeContext, Context};
use crate::tests::TestTrait;

use std::io::{Read,Write, Result as IoResult};
use core::ptr::NonNull;

#[test]
fn handshakecontext_sync() {
assert!(!TestTrait::<dyn Sync, HandshakeContext>::new().impls_trait(), "HandshakeContext must be !Sync");
}

struct NonSendStream {
_buffer: NonNull<u8>,
}

impl Read for NonSendStream {
fn read(&mut self, _: &mut [u8]) -> IoResult<usize> {
unimplemented!()
}
}

impl Write for NonSendStream {
fn write(&mut self, _: &[u8]) -> IoResult<usize> {
unimplemented!()
}

fn flush(&mut self) -> IoResult<()> {
unimplemented!()
}
}


struct SendStream {
_buffer: Vec<u8>,
}

impl Read for SendStream {
fn read(&mut self, _: &mut [u8]) -> IoResult<usize> {
unimplemented!()
}
}

impl Write for SendStream {
fn write(&mut self, _: &[u8]) -> IoResult<usize> {
unimplemented!()
}

fn flush(&mut self) -> IoResult<()> {
unimplemented!()
}
}


#[test]
fn context_send() {
assert!(!TestTrait::<dyn Send, NonSendStream>::new().impls_trait(), "NonSendStream can't be send");
assert!(!TestTrait::<dyn Send, Context<NonSendStream>>::new().impls_trait(), "Context<NonSendStream> can't be send");

assert!(TestTrait::<dyn Send, SendStream>::new().impls_trait(), "SendStream is send");
assert!(TestTrait::<dyn Send, Context<SendStream>>::new().impls_trait(), "Context<SendStream> is send");
}

}

// ssl_get_alpn_protocol
Expand Down
8 changes: 2 additions & 6 deletions mbedtls/tests/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ impl<T> TlsStream<T> {
}
}

unsafe impl<T> Send for TlsStream<T> {}
unsafe impl<T> Sync for TlsStream<T> {}


impl<T: io::Read + io::Write + 'static> io::Read for TlsStream<T>
impl<T: io::Read + io::Write> io::Read for TlsStream<T>
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.context.lock().unwrap().read(buf)
}
}

impl<T: io::Read + io::Write + 'static> io::Write for TlsStream<T>
impl<T: io::Read + io::Write> io::Write for TlsStream<T>
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.context.lock().unwrap().write(buf)
Expand Down

0 comments on commit 020e01d

Please sign in to comment.