4

I am using https://docs.rs/mysql/19.0.1/mysql/ to fetch some rows from a mySQL-Database. I assign them to a struct like this:

use mysql::*;
use mysql::prelude::*;
use serde::Serialize;
 
#[derive(Debug, PartialEq, Eq, Serialize)]
pub struct Policy {
    sub: Option<mysql::Binary>,
    contents: Option<String>,
}
 
pub fn list_policies() -> Result<Vec<Policy>> {
    let url = "";
    let pool = Pool::new(url)?;
    let mut connection = pool.get_conn()?;
 
    let policies: Vec<Policy> = connection.query_map("SELECT sub, contents FROM policy", |(sub, contents)| {
        Policy { sub, contents }
    },)?;
 
    Ok(policies)
}

Problem is that some of my rows are stored in mysql binary format (uuid binary(16) for example.) I discovered the mysql::Binary but when I want to use it in my struct, I get the following error:

error[E0369]: binary operation `==` cannot be applied to type `std::option::Option<mysql::Binary>`
 --> src/database/mod.rs:7:2
  |
7 |     sub: Option<mysql::Binary>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

and a few similar ones with "Eq" etc.

My question is: How can I fetch binary-columns with the rust mysql-package? I can't find anything using the provided documentation. Any example would be great.

1 Answers1

3

Maybe you can read your data as a vec of bytes like the following

pub struct Policy {
    sub: Option<Vec<u8>>,
    contents: Option<String>,
}
edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • I did, now I try to convert it back into a string. In node, I could use Buffers for that. Do you know how to do it here? – CodingVampyre Sep 28 '20 at 07:28
  • Could you please ask the question in a new separated thread ? [This answer](https://stackoverflow.com/questions/19076719/how-do-i-convert-a-vector-of-bytes-u8-to-a-string) might help – edkeveked Sep 28 '20 at 07:56