RelationDef & RelationBuilder are Send & Sync (#898)

* `RelationDef` & `RelationBuilder` are `Send` & `Sync`

* [issues] add tests

Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
This commit is contained in:
Billy Chan 2022-07-21 23:35:20 +08:00 committed by GitHub
parent 41116499e0
commit 83c6e4a4db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 5 deletions

17
issues/892/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[workspace]
# A separate workspace
[package]
name = "sea-orm-issues-892"
version = "0.1.0"
authors = []
edition = "2021"
publish = false
[dependencies]
tokio = { version = "1.20.0", features = ["rt-multi-thread", "macros"] }
[dependencies.sea-orm]
path = "../../" # remove this line in your own project
features = ["runtime-tokio-rustls", "sqlx-sqlite", "macros"]
default-features = false

19
issues/892/src/main.rs Normal file
View File

@ -0,0 +1,19 @@
use sea_orm::tests_cfg::{cake, cake_filling};
use sea_orm::{Database, DbErr, EntityTrait, JoinType, QuerySelect, RelationTrait};
#[tokio::main]
async fn main() -> Result<(), DbErr> {
let db = Database::connect("sqlite::memory:").await?;
tokio::spawn(async move {
let _cakes = cake::Entity::find()
.join_rev(JoinType::InnerJoin, cake_filling::Relation::Cake.def())
.all(&db)
.await
.unwrap();
})
.await
.unwrap();
Ok(())
}

View File

@ -62,7 +62,7 @@ pub struct RelationDef {
/// `UPDATE` Operation is performed
pub on_update: Option<ForeignKeyAction>,
/// Custom join ON condition
pub on_condition: Option<Box<dyn Fn(DynIden, DynIden) -> Condition>>,
pub on_condition: Option<Box<dyn Fn(DynIden, DynIden) -> Condition + Send + Sync>>,
/// The name of foreign key constraint
pub fk_name: Option<String>,
}
@ -85,7 +85,7 @@ impl std::fmt::Debug for RelationDef {
fn debug_on_condition(
d: &mut core::fmt::DebugStruct<'_, '_>,
on_condition: &Option<Box<dyn Fn(DynIden, DynIden) -> Condition>>,
on_condition: &Option<Box<dyn Fn(DynIden, DynIden) -> Condition + Send + Sync>>,
) {
match on_condition {
Some(func) => {
@ -118,7 +118,7 @@ where
is_owner: bool,
on_delete: Option<ForeignKeyAction>,
on_update: Option<ForeignKeyAction>,
on_condition: Option<Box<dyn Fn(DynIden, DynIden) -> Condition>>,
on_condition: Option<Box<dyn Fn(DynIden, DynIden) -> Condition + Send + Sync>>,
fk_name: Option<String>,
}
@ -190,7 +190,7 @@ impl RelationDef {
/// );
pub fn on_condition<F>(mut self, f: F) -> Self
where
F: Fn(DynIden, DynIden) -> Condition + 'static,
F: Fn(DynIden, DynIden) -> Condition + 'static + Send + Sync,
{
self.on_condition = Some(Box::new(f));
self
@ -270,7 +270,7 @@ where
/// denoting the left-hand side and right-hand side table in the join expression.
pub fn on_condition<F>(mut self, f: F) -> Self
where
F: Fn(DynIden, DynIden) -> Condition + 'static,
F: Fn(DynIden, DynIden) -> Condition + 'static + Send + Sync,
{
self.on_condition = Some(Box::new(f));
self
@ -303,3 +303,19 @@ where
}
}
}
#[cfg(test)]
mod tests {
use crate::{
tests_cfg::{cake, fruit},
RelationBuilder, RelationDef,
};
#[test]
fn assert_relation_traits() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<RelationDef>();
assert_send_sync::<RelationBuilder<cake::Entity, fruit::Entity>>();
}
}