mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-23 18:40:24 +00:00
feat(core): add minimal Runtime and blocking::Runtime and re-expose sqlx-core in sqlx
This commit is contained in:
6
sqlx-core/src/blocking.rs
Normal file
6
sqlx-core/src/blocking.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
//! Types and traits used to implement a database driver with **blocking** I/O.
|
||||
//!
|
||||
|
||||
pub(crate) mod runtime;
|
||||
|
||||
pub use runtime::Runtime;
|
||||
25
sqlx-core/src/blocking/runtime.rs
Normal file
25
sqlx-core/src/blocking/runtime.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use std::io;
|
||||
use std::net::TcpStream;
|
||||
|
||||
/// Describes a set of types and functions used to open and manage
|
||||
/// resources within SQLx using blocking I/O.
|
||||
///
|
||||
pub trait Runtime {
|
||||
type TcpStream;
|
||||
|
||||
/// Opens a TCP connection to a remote host at the specified port.
|
||||
fn connect_tcp(host: &str, port: u16) -> io::Result<Self::TcpStream>;
|
||||
}
|
||||
|
||||
/// Uses the `std::net` primitives to implement a blocking runtime for SQLx.
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "blocking")))]
|
||||
#[derive(Debug)]
|
||||
pub struct Blocking;
|
||||
|
||||
impl Runtime for Blocking {
|
||||
type TcpStream = TcpStream;
|
||||
|
||||
fn connect_tcp(host: &str, port: u16) -> io::Result<Self::TcpStream> {
|
||||
TcpStream::connect((host, port))
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! SQLx Core (`sqlx-core`) is the core set of traits and types that are used and implemented for each
|
||||
//! database driver (`sqlx-postgres`, `sqlx-mysql`, etc.).
|
||||
//!
|
||||
#![cfg_attr(doc_cfg, feature(doc_cfg))]
|
||||
#![deny(unsafe_code)]
|
||||
#![warn(rust_2018_idioms)]
|
||||
#![warn(future_incompatible)]
|
||||
@@ -16,3 +17,34 @@
|
||||
#![warn(clippy::use_self)]
|
||||
#![warn(clippy::useless_let_if_seq)]
|
||||
#![allow(clippy::clippy::doc_markdown)]
|
||||
|
||||
// crate renames to allow the feature name "tokio" and "async-std" (as features
|
||||
// can't directly conflict with dependency names)
|
||||
|
||||
#[cfg(feature = "async-std")]
|
||||
extern crate _async_std as async_std;
|
||||
|
||||
#[cfg(feature = "tokio")]
|
||||
extern crate _tokio as tokio;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
mod runtime;
|
||||
|
||||
#[cfg(feature = "blocking")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "blocking")))]
|
||||
pub mod blocking;
|
||||
|
||||
#[cfg(feature = "blocking")]
|
||||
pub use blocking::runtime::Blocking;
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
pub use runtime::Runtime;
|
||||
|
||||
#[cfg(all(feature = "async", feature = "async-std"))]
|
||||
pub use runtime::async_std::AsyncStd;
|
||||
|
||||
#[cfg(all(feature = "async", feature = "tokio"))]
|
||||
pub use runtime::tokio::Tokio;
|
||||
|
||||
#[cfg(all(feature = "async", feature = "actix"))]
|
||||
pub use runtime::actix::Actix;
|
||||
|
||||
25
sqlx-core/src/runtime.rs
Normal file
25
sqlx-core/src/runtime.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use std::io;
|
||||
|
||||
use futures_util::future::BoxFuture;
|
||||
|
||||
#[cfg(feature = "async-std")]
|
||||
pub(crate) mod async_std;
|
||||
|
||||
#[cfg(feature = "actix")]
|
||||
pub(crate) mod actix;
|
||||
|
||||
#[cfg(feature = "tokio")]
|
||||
pub(crate) mod tokio;
|
||||
|
||||
/// Describes a set of types and functions used to open and manage
|
||||
/// resources within SQLx using asynchronous I/O.
|
||||
#[cfg_attr(
|
||||
doc_cfg,
|
||||
doc(cfg(any(feature = "async-std", feature = "tokio", feature = "actix")))
|
||||
)]
|
||||
pub trait Runtime {
|
||||
type TcpStream;
|
||||
|
||||
/// Opens a TCP connection to a remote host at the specified port.
|
||||
fn connect_tcp(host: &str, port: u16) -> BoxFuture<'_, io::Result<Self::TcpStream>>;
|
||||
}
|
||||
24
sqlx-core/src/runtime/actix.rs
Normal file
24
sqlx-core/src/runtime/actix.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use std::io;
|
||||
|
||||
use actix_rt::net::TcpStream;
|
||||
use futures_util::{future::BoxFuture, FutureExt};
|
||||
|
||||
use crate::runtime::Runtime;
|
||||
|
||||
/// Actix SQLx runtime. Uses [`actix-rt`][actix_rt] to provide [`Runtime`].
|
||||
///
|
||||
/// As of 2021 Jan., Actix re-exports Tokio so this should be equivalent to [`Tokio`][crate::Tokio].
|
||||
/// This is split-out to allow Actix to shift, or for it to use a different major Tokio version and
|
||||
/// still work with SQLx.
|
||||
///
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "actix")))]
|
||||
#[derive(Debug)]
|
||||
pub struct Actix;
|
||||
|
||||
impl Runtime for Actix {
|
||||
type TcpStream = TcpStream;
|
||||
|
||||
fn connect_tcp(host: &str, port: u16) -> BoxFuture<'_, io::Result<Self::TcpStream>> {
|
||||
TcpStream::connect((host, port)).boxed()
|
||||
}
|
||||
}
|
||||
28
sqlx-core/src/runtime/async_std.rs
Normal file
28
sqlx-core/src/runtime/async_std.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use std::io;
|
||||
|
||||
use async_std::{net::TcpStream, task::block_on};
|
||||
use futures_util::{future::BoxFuture, FutureExt};
|
||||
|
||||
use crate::runtime::Runtime;
|
||||
|
||||
/// [`async-std`](async_std) implementation of [`Runtime`].
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "async-std")))]
|
||||
#[derive(Debug)]
|
||||
pub struct AsyncStd;
|
||||
|
||||
impl Runtime for AsyncStd {
|
||||
type TcpStream = TcpStream;
|
||||
|
||||
fn connect_tcp(host: &str, port: u16) -> BoxFuture<'_, io::Result<Self::TcpStream>> {
|
||||
TcpStream::connect((host, port)).boxed()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "blocking")]
|
||||
impl crate::blocking::Runtime for AsyncStd {
|
||||
type TcpStream = TcpStream;
|
||||
|
||||
fn connect_tcp(host: &str, port: u16) -> io::Result<Self::TcpStream> {
|
||||
block_on(<AsyncStd as Runtime>::connect_tcp(host, port))
|
||||
}
|
||||
}
|
||||
22
sqlx-core/src/runtime/tokio.rs
Normal file
22
sqlx-core/src/runtime/tokio.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use std::io;
|
||||
|
||||
use futures_util::{future::BoxFuture, FutureExt};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::runtime::Runtime;
|
||||
|
||||
/// Tokio SQLx runtime. Uses [`tokio`] to provide [`Runtime`].
|
||||
///
|
||||
/// SQLx does not require the use of a multi-threaded executor.
|
||||
///
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "tokio")))]
|
||||
#[derive(Debug)]
|
||||
pub struct Tokio;
|
||||
|
||||
impl Runtime for Tokio {
|
||||
type TcpStream = TcpStream;
|
||||
|
||||
fn connect_tcp(host: &str, port: u16) -> BoxFuture<'_, io::Result<Self::TcpStream>> {
|
||||
TcpStream::connect((host, port)).boxed()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user