How to ignore field in rust struct

I have a UserSession struct, and a User struct. I want to cache a pointer to the user from the UserSession struct to the User struct. To pass them around together, i.e.:

#[derive(Debug, FromRow, SerializeRow)]
pub struct UserSession {
    pub uid: String,
    pub lang: Option<String>,
    pub created: i64,
    pub accessed: i64,
    pub ip: String,
    pub user_uid: String,

    // ignore this
    pub info: Option<User>,

However, there is something about the User object, the driver doesn’t like, I now get this error in rust:

30 |     pub info: Option<User>,
   |               ^^^^^^^^^^^^^^ the trait `FromCqlVal<Option<CqlValue>>` is not implemented for `Option<User>`

I have spent some time trying to work out how to make this error go away, I notice that there is something called #[scylla(skip)] but it seems not useful in this case.

How can I make the driver either ignore that cache info:Option<User> field, or make the info:Option<User> serializable so that the error goes away (even though I don’t need to serialize it).

I realize a workaround would be to create two versions of the Session struct, and copy out of that a Session2 struct with the cached field, but why do extra memory copies if we dont need to right? :slight_smile:

#[scylla(skip)] is (currently) attribute only working with serialization, not deserialization. I don’t think we have any similar attribute for deserialization right now, but there is currently a big deserialization refactor going on. I added a comment to remember about #[scylla(skip)]: Deserialization refactor: macros for the new traits · Issue #962 · scylladb/scylla-rust-driver · GitHub .

As for what you can do now: create a separate DBUserSession without this field and impl From<DBUserSession> for UserSession is one solution, but with a drawback you mentioned. Tthe performance impact should however be negligible, I think worrying about it now is unnecessary.

Another solution would be to implement FromRow yourself. Look at the generated implementation for the struct without info field, copy the implementation and add info field initialization to None.

1 Like

Thanks for the info. I was hoping there was an easy answer and I was just missing it in the documentation.

I just implemented a workaround for now (defined a new struct to combine pointers to a session and user struct).

Much appreciated.

1 Like