pub trait IntoExpr {
// Required method
fn into_expr(self) -> Expr;
}Expand description
Anything that can stand where an expression is expected.
This is bob’s progressive enhancement, made a trait: a &'static str is raw
SQL, a number is a SQL literal, a Value is a bound argument, and an
Expr is itself. Every builder slot in keelson takes
impl IntoExpr, so the same call site accepts a hand-written fragment today
and a structured expression tomorrow with no change in shape.
§Which conversion each type gets, and why
| from | to | rationale |
|---|---|---|
Expr | itself | |
&'static str, String, Cow<'static, str> | Expr::Raw | bob writes a Go string verbatim |
i8..i64, u8..u64, f32, f64, bool | Expr::Raw | bob’s fallback formats any other value into the SQL, which is what makes LIMIT 20 a literal |
Value | Expr::Arg | a Value is data, and data is bound, never interpolated |
DynExpr | Expr::Custom | the escape hatch for a dialect’s own shape |
The split on the last two rows is the whole safety story: text that the
program wrote is SQL, and text that came from outside is a Value. There is
deliberately no impl turning &str into a bound string — that would make the
same call site mean two different things depending on the author’s intent, and
bob does not do it either.
§Why &'static str and not &str
An Expr stores Cow<'static, str>, so that no public type needs a
lifetime parameter. A &'static str — which is what a literal is — borrows
for free; a shorter-lived &str would have to be copied on every conversion,
silently allocating for the overwhelmingly common literal case. Pass a
String when the text is computed.
Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".