rust

Pattern binding the same variable to different types sharing a trait

a 夏天 提交于 2021-02-11 14:49:10
问题 I have a question about pattern matching on values sharing some behaviour through a trait. I have an enum with two variants, each binding value of different types, where both types implement a trait. I'm trying to figure out whether it's possible to create a single pattern (of the E::VarA(x) | E::VarB(x) form) in which I bind both types to a single constant, provided I'm only interested in using the shared behaviour. An illustrative example: Playground: trait T { fn f(&self) -> usize; }

How to list a project's source files using the cargo crate?

谁都会走 提交于 2021-02-11 14:37:50
问题 I'm trying to list the source files of a Rust project using the cargo crate. I can not just simply list all the .rs files present in a directory as I want to retrieve exactly the files that the compiler sees during the compilation, which may not be all the .rs files. I'm conducting my experiments on the the Alacritty repository, which has a cargo workspace of 3 projects. Here is my code so far: extern crate cargo; use std::path::Path; use cargo::core::Source; fn main() { let path = Path::new(

&[u8] cannot be indexed by RangeFull?

北城以北 提交于 2021-02-11 14:20:00
问题 I am trying to create a struct which can contain anything that is sliceable to [u8] . use std::ops::{Index, RangeFull}; struct Wrapper<DataT> where DataT: ?Sized + Index<RangeFull, Output = [u8]>, { data: DataT, } impl<DataT> Wrapper<DataT> where DataT: ?Sized + Index<RangeFull, Output = [u8]>, { fn subwrapper(&self) -> Wrapper<Vec<u8>> { let mut buffer = Vec::<u8>::new(); buffer.extend_from_slice(&self.data[..]); Wrapper { data: buffer, } } } fn main() { let data : [u8; 3] = [1, 2, 3]; let w

How to handle “cannot infer an appropriate lifetime for autoref due to conflicting requirements”

半城伤御伤魂 提交于 2021-02-11 13:38:58
问题 I have this method from ExchangeInfo struct that returns a RwLockReadGuardRef from owning_ref crate: pub fn get_pair<'a, 'me: 'a>( &'me self, name: &str, ) -> RwLockReadGuardRef<'a, TradePairHashMap, Arc<RwLock<TradePair>>> { RwLockReadGuardRef::new(self.pairs.read().unwrap()).map(|pairs| pairs.get(name).unwrap()) } I'm trying to call it here: pub async fn get_pair<'a>( name: &str, exchange_info: &'a ExchangeInfo, retrieval: &dyn ExchangeInfoRetrieval, refresh: bool, ) -> Result<OwningRef<&'a

Why can't I store a value and a reference to that value in the same struct?

我们两清 提交于 2021-02-11 13:30:04
问题 I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_combined<'a>() -> Combined<'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and I want to store that value and a reference to that value in the same structure: struct Combined<'a>(Thing, &'a Thing); fn make_combined<'a>() -> Combined<'a> { let thing = Thing::new();

How to deserialize a TOML table containing an array of tables

馋奶兔 提交于 2021-02-11 09:11:38
问题 Take the following TOML data: [[items]] foo = 10 bar = 100 [[items]] foo = 12 bar = 144 And the following rust code: use serde_derive::Deserialize; use toml::from_str; use toml::value::Table; #[derive(Deserialize)] struct Item { foo: String, bar: String } fn main() { let items_string: &str = "[[items]]\nfoo = 10\nbar = 100\n\n[[items]]\nfoo = 12\nbar = 144\n"; let items_table: Table = from_str(items_string).unwrap(); let items: Vec<Item> = items_table["items"].as_array().unwrap().to_vec(); //

How to deserialize a TOML table containing an array of tables

廉价感情. 提交于 2021-02-11 09:11:27
问题 Take the following TOML data: [[items]] foo = 10 bar = 100 [[items]] foo = 12 bar = 144 And the following rust code: use serde_derive::Deserialize; use toml::from_str; use toml::value::Table; #[derive(Deserialize)] struct Item { foo: String, bar: String } fn main() { let items_string: &str = "[[items]]\nfoo = 10\nbar = 100\n\n[[items]]\nfoo = 12\nbar = 144\n"; let items_table: Table = from_str(items_string).unwrap(); let items: Vec<Item> = items_table["items"].as_array().unwrap().to_vec(); //

How to deserialize a TOML table containing an array of tables

狂风中的少年 提交于 2021-02-11 09:08:31
问题 Take the following TOML data: [[items]] foo = 10 bar = 100 [[items]] foo = 12 bar = 144 And the following rust code: use serde_derive::Deserialize; use toml::from_str; use toml::value::Table; #[derive(Deserialize)] struct Item { foo: String, bar: String } fn main() { let items_string: &str = "[[items]]\nfoo = 10\nbar = 100\n\n[[items]]\nfoo = 12\nbar = 144\n"; let items_table: Table = from_str(items_string).unwrap(); let items: Vec<Item> = items_table["items"].as_array().unwrap().to_vec(); //

How to deserialize a TOML table containing an array of tables

随声附和 提交于 2021-02-11 09:08:16
问题 Take the following TOML data: [[items]] foo = 10 bar = 100 [[items]] foo = 12 bar = 144 And the following rust code: use serde_derive::Deserialize; use toml::from_str; use toml::value::Table; #[derive(Deserialize)] struct Item { foo: String, bar: String } fn main() { let items_string: &str = "[[items]]\nfoo = 10\nbar = 100\n\n[[items]]\nfoo = 12\nbar = 144\n"; let items_table: Table = from_str(items_string).unwrap(); let items: Vec<Item> = items_table["items"].as_array().unwrap().to_vec(); //

Failed to cross-compile library from Windows to Android

◇◆丶佛笑我妖孽 提交于 2021-02-11 07:13:13
问题 I'm trying to cross-compile a Rust library to 4 Android targets (i386, x86_64, armv7, and aarch64) from Windows. I have set up Android NDK toolchain for these targets and tried to compile my library with help of this tutorial. I think I've set up the toolchain correctly, and I'm able to successfully execute aarch64-linux-android-clang in the terminal. Build log: cargo build --target aarch64-linux-android --release --verbose Fresh cc v1.0.28 Fresh autocfg v0.1.1 Fresh version_check v0.1.5