pub trait RowWrite: for<'a> Row<Value<'a>: Serialize> { }Expand description
Represents a row that can be writted into the database.
This trait is implemented automatically for all Row + Serialize types.
The main purpose of this trait is to simplify writing generic code.
§Examples
Let’s say we want to write a function that insert the provided batch of rows:
use clickhouse::{Client, RowOwned, RowWrite, error::Result};
async fn write_batch<R: RowOwned + RowWrite>(
table: &str,
client: Client,
data: &[R],
) -> Result<()> {
let mut insert = client.insert::<R>(table).await?;
for row in data {
insert.write(row).await?;
}
insert.end().await
}
// Usage
#[derive(clickhouse::Row, serde::Serialize)]
struct SomeRow { a: u32, b: String }
write_batch::<SomeRow>("table", client, &[/* ... */]).await?;However, this code works only for rows that do not hold any references. To support also rows that borrows data avoiding extra allocations, the signature should be changed to less intuitive one:
async fn write_batch<R: Row + RowWrite>( //<<< Row instead of RowOwned
table: &str,
client: Client,
data: &[R::Value<'_>], //<<< R::Value instead of R
) -> Result<()> {
/* same code */
}
// Usage
#[derive(Row, serde::Serialize)]
struct SomeRow<'a> { a: u32, b: &'a str }
let (first_b, second_b) = ("first", "second");
let rows = [SomeRow { a: 0, b: first_b }, SomeRow { a: 1, b: second_b }];
write_batch::<SomeRow>("table", client, &rows).await?;We use Row instead of RowOwned and R::Value<'_> instead of R here.
The last one is actually the same R but with a changed lifetime restricted to data.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.