chore(core): switch to (maintained) bytestring::ByteString from string::String<Bytes>

This commit is contained in:
Ryan Leckey 2021-01-08 15:26:39 -08:00
parent e8ea502cbb
commit 91fa554063
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9
2 changed files with 29 additions and 23 deletions

View File

@ -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<Bytes>;
unsafe fn get_str_unchecked(&mut self, n: usize) -> ByteString;
#[allow(unsafe_code)]
unsafe fn get_str_nul_unchecked(&mut self) -> io::Result<String<Bytes>>;
unsafe fn get_str_nul_unchecked(&mut self) -> io::Result<ByteString>;
}
impl BufExt for Bytes {
#[allow(unsafe_code)]
unsafe fn get_str_unchecked(&mut self, n: usize) -> String<Bytes> {
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<String<Bytes>> {
unsafe fn get_str_nul_unchecked(&mut self) -> io::Result<ByteString> {
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)))
}
}

View File

@ -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<Self::TcpStream>>;
}
#[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 = ();
}