rust

Why do I get an UnsupportedType error when serializing to TOML with a manually implemented Serialize for an enum with struct variants?

纵饮孤独 提交于 2021-01-27 07:00:11
问题 I'm trying to implement Serialize for an enum that includes struct variants. The serde.rs documentation indicates the following: enum E { // Use three-step process: // 1. serialize_struct_variant // 2. serialize_field // 3. end Color { r: u8, g: u8, b: u8 }, // Use three-step process: // 1. serialize_tuple_variant // 2. serialize_field // 3. end Point2D(f64, f64), // Use serialize_newtype_variant. Inches(u64), // Use serialize_unit_variant. Instance, } With that in mind, I proceeded to

Why do I get an UnsupportedType error when serializing to TOML with a manually implemented Serialize for an enum with struct variants?

只谈情不闲聊 提交于 2021-01-27 06:55:35
问题 I'm trying to implement Serialize for an enum that includes struct variants. The serde.rs documentation indicates the following: enum E { // Use three-step process: // 1. serialize_struct_variant // 2. serialize_field // 3. end Color { r: u8, g: u8, b: u8 }, // Use three-step process: // 1. serialize_tuple_variant // 2. serialize_field // 3. end Point2D(f64, f64), // Use serialize_newtype_variant. Inches(u64), // Use serialize_unit_variant. Instance, } With that in mind, I proceeded to

How do I fetch binary columns from MySQL in Rust?

旧街凉风 提交于 2021-01-27 06:31:10
问题 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,

Why can't I push into a Vec of dyn Trait unless I use a temporary variable?

余生长醉 提交于 2021-01-27 06:29:48
问题 This is my code: use std::rc::{Rc, Weak}; use std::cell::RefCell; trait Trait {} fn push<E: Trait>(e: E) { let mut v: Vec<Rc<RefCell<Box<dyn Trait>>>> = Vec::new(); // let x = Rc::new(RefCell::new(Box::new(e))); // v.push(x); // error v.push(Rc::new(RefCell::new(Box::new(e)))); // works fine } The v.push(x) raises this error: error[E0308]: mismatched types --> src/main.rs:12:12 | 7 | fn push<E: Trait>(e: E) { | - this type parameter ... 12 | v.push(x); | ^ expected trait object `dyn Trait`,

Why can't I push into a Vec of dyn Trait unless I use a temporary variable?

最后都变了- 提交于 2021-01-27 06:29:16
问题 This is my code: use std::rc::{Rc, Weak}; use std::cell::RefCell; trait Trait {} fn push<E: Trait>(e: E) { let mut v: Vec<Rc<RefCell<Box<dyn Trait>>>> = Vec::new(); // let x = Rc::new(RefCell::new(Box::new(e))); // v.push(x); // error v.push(Rc::new(RefCell::new(Box::new(e)))); // works fine } The v.push(x) raises this error: error[E0308]: mismatched types --> src/main.rs:12:12 | 7 | fn push<E: Trait>(e: E) { | - this type parameter ... 12 | v.push(x); | ^ expected trait object `dyn Trait`,

How do I fetch binary columns from MySQL in Rust?

时光怂恿深爱的人放手 提交于 2021-01-27 06:27:51
问题 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,

Is it possible to match against a NULL pointer in Rust?

若如初见. 提交于 2021-01-27 06:10:31
问题 Calling is_null() feels a bit odd: fn do_stuff(ptr: *const i32) -> Option<i32> { if ptr.is_null() { None } else { Some(do_transform(*ptr, 42)) } } 回答1: As of Rust 1.9, there's a function as_ref that converts a raw pointer to an Option<&T> , and a mutable variant as_mut: Your code would look something like fn do_stuff(ptr: *const i32) -> Option<i32> { let ptr = unsafe { ptr.as_ref() }; ptr.map(|x| do_transform(x, 42)) } 来源: https://stackoverflow.com/questions/37466676/is-it-possible-to-match

Is it possible to match against a NULL pointer in Rust?

淺唱寂寞╮ 提交于 2021-01-27 06:10:12
问题 Calling is_null() feels a bit odd: fn do_stuff(ptr: *const i32) -> Option<i32> { if ptr.is_null() { None } else { Some(do_transform(*ptr, 42)) } } 回答1: As of Rust 1.9, there's a function as_ref that converts a raw pointer to an Option<&T> , and a mutable variant as_mut: Your code would look something like fn do_stuff(ptr: *const i32) -> Option<i32> { let ptr = unsafe { ptr.as_ref() }; ptr.map(|x| do_transform(x, 42)) } 来源: https://stackoverflow.com/questions/37466676/is-it-possible-to-match

Is it possible to match against a NULL pointer in Rust?

为君一笑 提交于 2021-01-27 06:08:47
问题 Calling is_null() feels a bit odd: fn do_stuff(ptr: *const i32) -> Option<i32> { if ptr.is_null() { None } else { Some(do_transform(*ptr, 42)) } } 回答1: As of Rust 1.9, there's a function as_ref that converts a raw pointer to an Option<&T> , and a mutable variant as_mut: Your code would look something like fn do_stuff(ptr: *const i32) -> Option<i32> { let ptr = unsafe { ptr.as_ref() }; ptr.map(|x| do_transform(x, 42)) } 来源: https://stackoverflow.com/questions/37466676/is-it-possible-to-match

When adding `#![no_std]` to a library, are there any disadvantages or complications for the users of that library?

天涯浪子 提交于 2021-01-27 06:08:09
问题 I wrote a Rust library. I heard about the no_std feature and noticed that my library does not use anything from std that isn't offered by core and alloc. So in theory I could just add the #![no_std] attribute and change a few imports. But I wonder how this influences the users of my library. Of course, my hope is that by using #![no_std] , users in no_std environments can use my crate as well. That's good, of course. But: do users of my library have any disadvantage from my library being no