Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove bounds on establish, test that Send gets derived as needed #186

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 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,69 @@ impl HandshakeContext {

#[cfg(test)]
mod tests {
use crate::ssl::context::HandshakeContext;
use crate::tests::TestTrait;
#[cfg(feature = "std")]
use std::io::{Read,Write, Result as IoResult};

#[cfg(not(feature = "std"))]
use core_io::{Read, Write, Result as IoResult};

use crate::ssl::context::{HandshakeContext, Context};
use crate::tests::TestTrait;

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

struct NonSendStream {
_buffer: core::ptr::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: 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
10 changes: 2 additions & 8 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 Expand Up @@ -83,8 +79,6 @@ impl MbedSSLServer {
}
}

unsafe impl Send for MbedSSLServer {}

/// An abstraction to allow any SSL implementation to be used with server-side HttpsStreams.
impl<T> SslServer<T> for MbedSSLServer
where T: NetworkStream + Send + Clone + fmt::Debug + Sync
Expand Down