Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upDifference between two implementations of user types #1550
Comments
This comment has been minimized.
|
Your first impl won't work. It'll fail when you try to execute a query containing a |
sgrif
closed this
Feb 9, 2018
This comment has been minimized.
vityafx
commented
Feb 12, 2018
•
|
So I end up with this: #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)]
#[sql_type = "Integer"]
pub enum PunishmentType {
/// The user is locked from adding to game queues.
Locked,
/// The user can't speak but can read the chat.
Muted,
/// The user is banned from the server.
Banned,
}
impl From<i32> for PunishmentType {
fn from(value: i32) -> PunishmentType {
match value {
0 => PunishmentType::Locked,
1 => PunishmentType::Muted,
_ => PunishmentType::Banned,
}
}
}
impl<ST, DB> FromSql<ST, DB> for PunishmentType
where
i32: FromSql<ST, DB>,
DB: Backend,
{
fn from_sql(value: Option<&<DB as Backend>::RawValue>) -> deserialize::Result<Self> {
<i32 as FromSql<ST, DB>>::from_sql(value).map(PunishmentType::from)
}
}But as you said, when I try to execute a query I get problems:
How do I fix this? |
This comment has been minimized.
|
I'd need to see more of your schema to answer that, but I'm recovering from a concussion and can't help you right now. Try asking in gitter |
This comment has been minimized.
|
Oh also it looks like the problem is that you don't have a |
This comment has been minimized.
vityafx
commented
Feb 12, 2018
|
When I had it there was another error:
My table is: #[derive(Debug, Clone, Queryable, Insertable)]
#[table_name = "punishments"]
pub struct Punishment {
/// The ID of the punishment in the table.
pub id: i32,
/// The punished user id
pub user_id: i64,
/// The server id on which the user is punished on
pub server_id: i64,
/// The punishment start time
pub started_at: NaiveDateTime,
/// The punishment duration
pub duration: chrono::Duration,
/// The punishment reason
pub reason: String,
/// The punishment type
pub punishment_type: PunishmentType,
/// The processed mark. The punishment is processed when it is expired and all the things were done. This mark
/// can also be used for checking whether it is needed to do something if the punishment is expired or not.
pub processed: bool,
}and schema: table! {
punishments (id) {
id -> Integer,
user_id -> BigInt,
server_id -> BigInt,
started_at -> Timestamp,
duration -> BigInt,
reason -> Text,
punishment_type -> Integer,
processed -> Bool,
}
}Thank you for trying to help me :) |
This comment has been minimized.
|
The following code works for me: #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)]
#[sql_type = "Integer"]
pub enum PunishmentType {
/// The user is locked from adding to game queues.
Locked = 1,
/// The user can't speak but can read the chat.
Muted = 2,
/// The user is banned from the server.
Banned = 3,
}
impl From<i32> for PunishmentType {
fn from(value: i32) -> PunishmentType {
match value {
0 => PunishmentType::Locked,
1 => PunishmentType::Muted,
_ => PunishmentType::Banned,
}
}
}
impl<DB> FromSql<Integer, DB> for PunishmentType
where
i32: FromSql<Integer, DB>,
DB: Backend,
{
fn from_sql(value: Option<&<DB as Backend>::RawValue>) -> deserialize::Result<Self> {
<i32 as FromSql<Integer, DB>>::from_sql(value).map(PunishmentType::from)
}
}
impl<DB> ToSql<Integer, DB> for PunishmentType
where
DB: Backend
{
fn to_sql<W: Write>(&self, out: &mut serialize::Output<W, DB>) -> serialize::Result {
<i32 as ToSql<Integer, DB>>::to_sql(&(*self as i32), out)
}
} |
This comment has been minimized.
vityafx
commented
Feb 12, 2018
|
@weiznich That did! How did it work? All you changed was a generic type to specific one... |
vityafx commentedFeb 9, 2018
I have tried implementing a mapping for my custom type by my own at first. Then I tried to do the same according to the example in this repository. However, both of ways seem to work. Could anyone outline differences for me?
First way:
Second way:
I mean, if there is no difference, what is the
Proxy-structure is used for? Thanks.