rust

How do I create a proc_macro_attribute?

南笙酒味 提交于 2020-01-11 07:13:07
问题 Now that proc_macros have been stabilized, how does one create such a thing? From what I've seen, there's the option of putting a #[proc_macro_attribute] annotation on a fn whatsitsname(attrs: TokenStream, code: TokenStream) -> TokenStream , but how can I register it? How can I add custom attributes? 回答1: The Rust compiler has a fairly complete test suite. When looking for examples of newly-introduced features, I frequently start there: $ rg -c proc_macro_attribute src/test/run-pass-fulldeps

Simple organization of Rust traits for “polymorphic” return

試著忘記壹切 提交于 2020-01-11 06:38:07
问题 I have a basic struct called Frame that is useful for a bunch of calculations:. pub struct Frame<T> { grid_val: Vec<T>, grid_space: Vec<[T; 2]>, calculated_result: Option<Vec<T>> } Frame can be used to describe most basic calculations, but sometimes there's more complicated issues that come up and I need to add some more geometric information. So I used composition for each geometry: pub struct Sphere<T> { grid: Frame<T>, radius: T } pub struct Hyperbola<T> { top_grid: Frame<T>, bottom_grid:

Structure containing fields that know each other

心不动则不痛 提交于 2020-01-11 05:48:25
问题 I have a set of objects that need to know each other to cooperate. These objects are stored in a container. I'm trying to get a very simplistic idea of how to architecture my code in Rust. Let's use an analogy. A Computer contains: 1 Mmu 1 Ram 1 Processor In Rust: struct Computer { mmu: Mmu, ram: Ram, cpu: Cpu, } For anything to work, the Cpu needs to know about the Mmu it is linked to, and the Mmu needs to know the Ram it is linked to. I do not want the Cpu to aggregate by value the Mmu .

How can I convert toml-rs result to std::collections::HashMap

僤鯓⒐⒋嵵緔 提交于 2020-01-11 05:42:08
问题 I'm new to Rust and trying to build something simple to get going. I want to load data from a .toml file and use rustache to render out some text from it. Rustache appears to take a HashMap as its data source, and I'm sure from looking at the toml-rs docs that I should be able to convert its Table and Array types to HashMap s and Vec s, and I suspect it's got something to do with Decoder , but I can't figure it out. If somebody could provide a short example of how to do this I would be very

Why doesn't a nested reference to an array coerce to a slice?

☆樱花仙子☆ 提交于 2020-01-11 05:15:30
问题 I read What are Rust's exact auto-dereferencing rules? from beginning to end, but I still have a question about the coercion from array to slice. Let us think about the following code: let arr: &[i32; 5] = &&&[1, 2, 3, 4, 5]; // let arr: &[i32] = &&&[1, 2, 3, 4, 5]; // Error; expected slice, found reference I would expect that &&&[1, 2, 3, 4, 5] has the type, &&&[i32; 5] and dereferences to &&[i32; 5] => &[i32; 5] => &[i32; 5] => &[i32] , but the result is different from what I expected. I

cargo test --release causes a stack overflow. Why doesn't cargo bench?

浪尽此生 提交于 2020-01-11 04:50:06
问题 In trying to write an optimized DSP algorithm, I was wondering about relative speed between stack allocation and heap allocation, and size limits of stack-allocated arrays. I realize there is a stack frame size limit, but I don't understand why the following runs, generating seemingly realistic benchmark results with cargo bench , but fails with a stack overflow when run with cargo test --release . #![feature(test)] extern crate test; #[cfg(test)] mod tests { use test::Bencher; #[bench] fn it

How to get pointer offset in bytes?

不打扰是莪最后的温柔 提交于 2020-01-11 03:08:11
问题 While raw pointers in Rust have the offset method, this only increments by the size of the pointer. How can I get access to the pointer in bytes? Something like this in C: var_offset = (typeof(var))((char *)(var) + offset); 回答1: From the answer I linked to your previous question: macro_rules! offset_of { ($ty:ty, $field:ident) => { unsafe { &(*(0 as *const $ty)).$field as *const _ as usize } } } fn main() { let p: *const Baz = 0x1248 as *const _; let p2: *const Foo = ((p as usize) - offset_of

cannot borrow as immutable because it is also borrowed as mutable

ⅰ亾dé卋堺 提交于 2020-01-11 02:42:11
问题 I'm using the structs Foo and Bar from a library and I'm getting a compilation error in the client code. I simplified the code to this: use std::marker::PhantomData; struct Foo { some_str: &'static str, } struct Bar<'a> { some_str: &'static str, marker: PhantomData<&'a Foo>, } impl Foo { fn read_data(&self) { // add code here } fn create_bar<'a>(&'a mut self) -> Bar<'a> { Bar { some_str: "test2", marker: PhantomData, } } } fn process(_arr: &mut [Bar]) {} fn main() { let mut foo = Foo { some

Rust can't find crate

隐身守侯 提交于 2020-01-10 17:32:09
问题 I'm trying to create a module in Rust and then use it from a different file. This is my file structure: matthias@X1:~/projects/bitter-oyster$ tree . ├── Cargo.lock ├── Cargo.toml ├── Readme.md ├── src │ ├── liblib.rlib │ ├── lib.rs │ ├── main.rs │ ├── main.rs~ │ └── plot │ ├── line.rs │ └── mod.rs └── target └── debug ├── bitter_oyster.d ├── build ├── deps ├── examples ├── libbitter_oyster.rlib └── native 8 directories, 11 files This is Cargo.toml: [package] name = "bitter-oyster" version =

Rust can't find crate

隐身守侯 提交于 2020-01-10 17:30:07
问题 I'm trying to create a module in Rust and then use it from a different file. This is my file structure: matthias@X1:~/projects/bitter-oyster$ tree . ├── Cargo.lock ├── Cargo.toml ├── Readme.md ├── src │ ├── liblib.rlib │ ├── lib.rs │ ├── main.rs │ ├── main.rs~ │ └── plot │ ├── line.rs │ └── mod.rs └── target └── debug ├── bitter_oyster.d ├── build ├── deps ├── examples ├── libbitter_oyster.rlib └── native 8 directories, 11 files This is Cargo.toml: [package] name = "bitter-oyster" version =