a_d*_*zle 0 postgresql rust rust-diesel
我在尝试创建 Diesel 默认情况下未实现的自定义类型时遇到了以下代码的U128困难u128。我的目标是将原u128语作为文本格式或数字(最好是数字)存储在 Postgresql 数据库中。
use std::io::Write;
use diesel::{AsExpression, deserialize, FromSqlRow, serialize};
use diesel::deserialize::{FromSql};
use diesel::pg::{Pg};
use diesel::serialize::{IsNull, Output, ToSql};
use diesel::sql_types::{Text};
#[derive(AsExpression, FromSqlRow, Debug, Clone, Copy)]
#[diesel(sql_type = Text)]
pub struct U128(pub u128);
impl ToSql<Text, Pg> for U128 {
fn to_sql<'b>(&self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
write!(out, "{}", self.0.to_string())?;
Ok(IsNull::No)
}
}
impl FromSql<Text, Pg> for U128 {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
let s = String::from_utf8_lossy(bytes.as_bytes());
Ok(U128(s.parse()?))
}
}
Run Code Online (Sandbox Code Playgroud)
这是我创建的柴油结构。
#[derive(Queryable, Selectable)]
#[diesel(table_name = crate::schema::balance)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Balance {
pub id: i32,
pub account_name: String,
pub balance: u128,
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试u128在柴油结构中使用时遇到的编译错误。
error[E0277]: the trait bound `u128: diesel::Queryable<diesel::sql_types::Text, Pg>` is not satisfied
--> src\models.rs:117:27
|
117 | pub balance: u128,
| ^^^^ the trait `diesel::Queryable<diesel::sql_types::Text, Pg>` is not implemented for `u128`
|
= help: the following other types implement trait `diesel::Queryable<ST, DB>`:
i8
i16
i32
i64
u8
u16
u32
u64
and 2 others
= note: required for `u128` to implement `FromSqlRow<diesel::sql_types::Text, Pg>`
= help: see issue #48214
Run Code Online (Sandbox Code Playgroud)
diesel doesn't magically apply any wrapper that might work, that's impossible, instead you have to tell it, which intermediate type it should use with (de)serialize_as attributes:
#[derive(Queryable, Selectable)]
#[diesel(table_name = crate::schema::balance)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Balance {
pub id: i32,
pub account_name: String,
#[diesel(serialize_as = U128, deserialize_as = U128)]
pub balance: u128,
}
Run Code Online (Sandbox Code Playgroud)
for that to work, diesel also needs a way to convert your wrapper from/to the target type, you can provide that with the From trait:
impl From<u128> for U128 {
fn from(v: u128) -> U128 {
U128(v)
}
}
impl From<U128> for u128 {
fn from (v: U128) -> u128 {
v.0
}
}
Run Code Online (Sandbox Code Playgroud)
Note: diesel uses TryInto for conversion, but since this one is infallible and there is a chain of blanket implementations that gets us there for free, I implemented From instead.
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |