From 91fa5540636065d5eb7f82c6a1a694714c5ca3d2 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 8 Jan 2021 15:26:39 -0800 Subject: [PATCH] chore(core): switch to (maintained) bytestring::ByteString from string::String --- sqlx-core/src/io/buf.rs | 14 +++++++------- sqlx-core/src/runtime.rs | 38 ++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/sqlx-core/src/io/buf.rs b/sqlx-core/src/io/buf.rs index 3910a1b8..b807673c 100644 --- a/sqlx-core/src/io/buf.rs +++ b/sqlx-core/src/io/buf.rs @@ -1,8 +1,8 @@ use std::io; use bytes::{Buf, Bytes}; +use bytestring::ByteString; use memchr::memchr; -use string::String; // UNSAFE: _unchecked string methods // intended for use when the protocol is *known* to always produce @@ -10,23 +10,23 @@ use string::String; pub trait BufExt: Buf { #[allow(unsafe_code)] - unsafe fn get_str_unchecked(&mut self, n: usize) -> String; + unsafe fn get_str_unchecked(&mut self, n: usize) -> ByteString; #[allow(unsafe_code)] - unsafe fn get_str_nul_unchecked(&mut self) -> io::Result>; + unsafe fn get_str_nul_unchecked(&mut self) -> io::Result; } impl BufExt for Bytes { #[allow(unsafe_code)] - unsafe fn get_str_unchecked(&mut self, n: usize) -> String { - String::from_utf8_unchecked(self.split_to(n)) + unsafe fn get_str_unchecked(&mut self, n: usize) -> ByteString { + ByteString::from_bytes_unchecked(self.split_to(n)) } #[allow(unsafe_code)] - unsafe fn get_str_nul_unchecked(&mut self) -> io::Result> { + unsafe fn get_str_nul_unchecked(&mut self) -> io::Result { let nul = memchr(b'\0', self).ok_or(io::ErrorKind::InvalidData)?; - Ok(String::from_utf8_unchecked(self.split_to(nul + 1).slice(..nul))) + Ok(ByteString::from_bytes_unchecked(self.split_to(nul + 1).slice(..nul))) } } diff --git a/sqlx-core/src/runtime.rs b/sqlx-core/src/runtime.rs index 3d1f6b77..494d506c 100644 --- a/sqlx-core/src/runtime.rs +++ b/sqlx-core/src/runtime.rs @@ -1,18 +1,27 @@ +#[cfg(feature = "_mock")] +#[doc(hidden)] +pub mod mock; + #[cfg(feature = "async-std")] -mod async_std; +#[path = "runtime/async_std.rs"] +mod async_std_; #[cfg(feature = "actix")] -mod actix; +#[path = "runtime/actix.rs"] +mod actix_; #[cfg(feature = "tokio")] -mod tokio; +#[path = "runtime/tokio.rs"] +mod tokio_; #[cfg(feature = "actix")] -pub use self::actix::Actix; +pub use actix_::Actix; #[cfg(feature = "async-std")] -pub use self::async_std::AsyncStd; +pub use async_std_::AsyncStd; +#[cfg(feature = "_mock")] +pub use mock::Mock; #[cfg(feature = "tokio")] -pub use self::tokio::Tokio; +pub use tokio_::Tokio; /// Describes a set of types and functions used to open and manage /// resources within SQLx. @@ -21,10 +30,7 @@ pub trait Runtime: 'static + Send + Sync { } #[cfg(feature = "async")] -pub trait AsyncRuntime: Runtime -where - Self::TcpStream: futures_io::AsyncRead, -{ +pub trait AsyncRuntime: Runtime { /// Opens a TCP connection to a remote host at the specified port. fn connect_tcp( host: &str, @@ -32,14 +38,14 @@ where ) -> futures_util::future::BoxFuture<'_, std::io::Result>; } -#[cfg(feature = "async")] -pub trait AsyncRead { - fn read(&mut self, buf: &mut [u8]) -> futures_util::future::BoxFuture<'_, u64>; -} - // when the async feature is not specified, this is an empty trait // we implement `()` for it to allow the lib to still compile -#[cfg(not(feature = "async"))] +#[cfg(not(any( + feature = "async_std", + feature = "actix", + feature = "tokio", + feature = "blocking" +)))] impl Runtime for () { type TcpStream = (); }