Skip to content

Commit 8346526

Browse files
authored
Make into_condition a shorthand for Into::<Condition>::into (#939)
## PR Info Implemented `IntoCondition` for `T: Into<Cond>`. If you have manually implemented this trait, it may cause conflicts. You should rewrite it as `impl From<..> for Condition`.
1 parent 6e598d2 commit 8346526

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ from_tbl: "foo".into_table_ref(),
251251

252252
### Minor breaking changes
253253

254+
* Implemented `IntoCondition` for `T: Into<Cond>`. If you have manually implemented this trait, it may cause conflicts. You should rewrite it as `impl From<..> for Condition`.
255+
254256
* Unboxed `Value` variants may cause compile error. Simply remove the `Box` in these cases https://github.com/SeaQL/sea-query/pull/925
255257
```rust
256258
error[E0308]: mismatched types

src/query/condition.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,39 @@ pub struct Condition {
1414
pub(crate) conditions: Vec<ConditionExpression>,
1515
}
1616

17-
pub trait IntoCondition {
18-
fn into_condition(self) -> Condition;
17+
pub type Cond = Condition;
18+
19+
impl From<Expr> for Condition {
20+
fn from(expr: Expr) -> Self {
21+
Condition {
22+
negate: false,
23+
condition_type: ConditionType::All,
24+
conditions: vec![ConditionExpression::Expr(expr)],
25+
}
26+
}
1927
}
2028

21-
pub type Cond = Condition;
29+
impl From<ConditionExpression> for Condition {
30+
fn from(ce: ConditionExpression) -> Self {
31+
Condition {
32+
negate: false,
33+
condition_type: ConditionType::All,
34+
conditions: vec![ce],
35+
}
36+
}
37+
}
38+
39+
/// A helper trait.
40+
///
41+
/// You shouldn't implement this manually.
42+
pub trait IntoCondition: Into<Condition> {
43+
#[inline(always)]
44+
fn into_condition(self) -> Condition {
45+
self.into()
46+
}
47+
}
48+
49+
impl<T> IntoCondition for T where T: Into<Condition> {}
2250

2351
/// An internal representation of conditions.
2452
/// May be refactored away in the future if we can get our head around it.
@@ -310,26 +338,6 @@ impl From<Expr> for ConditionExpression {
310338
}
311339
}
312340

313-
impl From<Expr> for Condition {
314-
fn from(condition: Expr) -> Self {
315-
Condition {
316-
negate: false,
317-
condition_type: ConditionType::All,
318-
conditions: vec![ConditionExpression::Expr(condition)],
319-
}
320-
}
321-
}
322-
323-
impl From<ConditionExpression> for Condition {
324-
fn from(ce: ConditionExpression) -> Self {
325-
Condition {
326-
negate: false,
327-
condition_type: ConditionType::All,
328-
conditions: vec![ce],
329-
}
330-
}
331-
}
332-
333341
/// Macro to easily create an [`Condition::any`].
334342
///
335343
/// # Examples
@@ -613,18 +621,6 @@ pub trait ConditionalStatement {
613621
C: IntoCondition;
614622
}
615623

616-
impl IntoCondition for Expr {
617-
fn into_condition(self) -> Condition {
618-
Condition::all().add(self)
619-
}
620-
}
621-
622-
impl IntoCondition for Condition {
623-
fn into_condition(self) -> Condition {
624-
self
625-
}
626-
}
627-
628624
impl ConditionHolder {
629625
pub fn new() -> Self {
630626
Self::default()

0 commit comments

Comments
 (0)