rust

How can I use serde to deserialize into a hierarchical decentralized configuration? [duplicate]

谁说我不能喝 提交于 2021-02-10 23:26:52
问题 This question already has answers here : How can deserialization of polymorphic trait objects be added in Rust if at all? (3 answers) How do I deserialize into trait, not a concrete type? (3 answers) Closed 6 months ago . I want to have a JSON config file of the form: { "general_option_foo": true, "general_option_bar": "hello world!", "modules": [ { "name": "moduleA", "module_options" : {"optFoo":"foo"} }, { "name" : "moduleB", "module_options" : {"optBar":[3,4], "optBuzz": true} } { "name":

Taking closures with explicit lifetimes as arguments in Rust

六眼飞鱼酱① 提交于 2021-02-10 19:56:26
问题 The closures section of Rust documentation has this example: fn call_with_ref<'a, F>(some_closure: F) -> i32 where F: Fn(&'a i32) -> i32 { let value = 0; some_closure(&value) } This doesn't compile because, as the docs put it: When a function has an explicit lifetime parameter, that lifetime must be at least as long as the entire call to that function. The borrow checker will complain that value doesn't live long enough, because it is only in scope after its declaration inside the function

Fast forward time when writing Substrate Runtime Test

拥有回忆 提交于 2021-02-10 18:44:53
问题 I am writing an auction type application with Substrate runtime. In writing test case, how can I fast forward the blockchain time (what's retrieved from <timestamp::Module<T>>::get() ) so I can reach the auction closing time and test the closing logic? Thanks. 回答1: You can just use Timestamp::set_timestamp to change the timestamp to whatever value you want. e.g. Timestamp::set_timestamp(42); You can check the tests for srml-timestamp for examples https://github.com/paritytech/substrate/blob

How to encode the hex string representation of an account id in Substrate using Rust?

感情迁移 提交于 2021-02-10 18:35:50
问题 Given a hex representation: 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d , we can get the AccountId it represents using keyring.encodeAddress() using JavaScript. However, what is the corresponding function in Rust? AccountId is the address of the Substrate user's address. For eg, 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY is the account id of Alice, from the Substrate's dev chain. 回答1: Within rust, you should not really start with the hex representation, you want

Why are len() and is_empty() not defined in a trait?

女生的网名这么多〃 提交于 2021-02-10 18:34:16
问题 Most patterns in Rust are captured by traits ( Iterator , From , Borrow , etc.). How come a pattern as pervasive as len / is_empty has no associated trait in the standard library? Would that cause problems which I do not foresee? Was it deemed useless? Or is it only that nobody thought of it (which seems unlikely)? 回答1: Was it deemed useless? I would guess that's the reason. What could you do with the knowledge that something is empty or has length 15? Pretty much nothing, unless you also

How do I specify the expected result of a `std::ops::Mul` in a trait bound?

守給你的承諾、 提交于 2021-02-10 18:01:35
问题 I have: use std::ops::{Add, Div, Mul, Neg, Sub}; pub trait Hilbert: Add + Sub + Mul + Div + Neg + Mul<f64> + Div<f64> + Sized { fn dot(&self, other: &Self) -> f64; fn magnitude(&self) -> f64; } fn g<T: Hilbert>(x: T) -> f64 { return (x * 2.0).dot(x); } ...which yields: error[E0599]: no method named `dot` found for type `<T as std::ops::Mul<f64>>::Output` in the current scope --> src/main.rs:9:22 | 9 | return (x * 2.0).dot(x); | ^^^ | = help: items from traits can only be used if the trait is

Is there a way to signal that an impl trait type also implements additional traits?

穿精又带淫゛_ 提交于 2021-02-10 17:46:02
问题 I have a function that returns an impl trait: pub fn new(buf: &[u8]) -> Result<impl Temperature, u8> Is there a way to signal that the underlying struct also implements Debug (via #[derive(...)] ), so I can format the value? 回答1: Yes, combine multiple traits with a + , just like in trait bounds: use std::fmt::Debug; trait Foo {} fn new() -> impl Foo + Debug { Dummy } #[derive(Debug)] struct Dummy; impl Foo for Dummy {} fn main() { println!("{:?}", new()); } 来源: https://stackoverflow.com

Is there a way to signal that an impl trait type also implements additional traits?

可紊 提交于 2021-02-10 17:45:07
问题 I have a function that returns an impl trait: pub fn new(buf: &[u8]) -> Result<impl Temperature, u8> Is there a way to signal that the underlying struct also implements Debug (via #[derive(...)] ), so I can format the value? 回答1: Yes, combine multiple traits with a + , just like in trait bounds: use std::fmt::Debug; trait Foo {} fn new() -> impl Foo + Debug { Dummy } #[derive(Debug)] struct Dummy; impl Foo for Dummy {} fn main() { println!("{:?}", new()); } 来源: https://stackoverflow.com

How do I conditionally return different types of futures?

无人久伴 提交于 2021-02-10 17:27:32
问题 I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future<Item = usize, Error = ()> { if 1 > 0 { future::ok(2).map(|x| x) } else { future::ok(10).and_then(|x| future::ok(x + 2)) } } This doesn't compile: error[E0308]: if and else have incompatible types --> src/lib.rs:6:5 | 6 | / if 1 > 0 { 7 | | future::ok(2).map(|x| x) 8 |

How do I conditionally return different types of futures?

柔情痞子 提交于 2021-02-10 17:27:28
问题 I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future<Item = usize, Error = ()> { if 1 > 0 { future::ok(2).map(|x| x) } else { future::ok(10).and_then(|x| future::ok(x + 2)) } } This doesn't compile: error[E0308]: if and else have incompatible types --> src/lib.rs:6:5 | 6 | / if 1 > 0 { 7 | | future::ok(2).map(|x| x) 8 |