rust

How do I use a macro across module files?

谁说我不能喝 提交于 2021-01-29 08:37:40
问题 I have two modules in separate files within the same crate, where the crate has macro_rules enabled. I want to use the macros defined in one module in another module. // macros.rs #[macro_export] // or not? is ineffectual for this, afaik macro_rules! my_macro(...) // something.rs use macros; // use macros::my_macro; <-- unresolved import (for obvious reasons) my_macro!() // <-- how? I currently hit the compiler error " macro undefined: 'my_macro' "... which makes sense; the macro system runs

Rust FFI, callbacks, and lifetimes

≡放荡痞女 提交于 2021-01-29 08:34:02
问题 I'm trying to build a nice rust wrapper around libuv, an event loop library written in C. I'm pretty much "done", but I'm having some trouble with callbacks and lifetimes. Being an event loop library, libuv relies heavily on callbacks. I have some code that can accept functions, closures, or a tuple of (obj, method-on-obj) and it handles creating an appropriate "trampoline" to get that across the FFI boundary. That all works. However, the problem I'm running into is that I cannot figure out

Attempting to use &self in a Rust program

你说的曾经没有我的故事 提交于 2021-01-29 08:14:20
问题 I have a file called info.rs that contains a small test structure intended to represent some basic file information. The code below is learning code for using structs: pub struct FileInfo { name: String, path: String, } impl FileInfo { pub fn new(aname: String,apath: String) { FileInfo { name: aname, path: apath } } pub fn get_name(&self) { self.name } pub fn get_path(&self) -> String { self.path } } According to documentation (and several examples!), the &self parameter used in the above

How to reuse codes for Binary Search Tree, Red-Black Tree, and AVL Tree?

孤人 提交于 2021-01-29 08:05:45
问题 I'm implementing BST (Binary Search Tree), RBT(Red-Black Tree), and AVLT (AVL Tree). I wrote a BST as follows: use std::cell::RefCell; use std::rc::Rc; use std::cmp::max; type RcRefBaseNode<T> = Rc<RefCell<BaseNode<T>>>; type BaseNodeLink<T> = Option<RcRefBaseNode<T>>; struct BaseNode<T: Ord> { pub key: T, left: BaseNodeLink<T>, right: BaseNodeLink<T>, } struct BaseTree<T: Ord> {root: BaseNodeLink<T>} impl<T: Ord> BaseTree<T> { fn new(data: T) -> Self { Self{ root: Some(Rc::new(RefCell::new

How to reuse codes for Binary Search Tree, Red-Black Tree, and AVL Tree?

末鹿安然 提交于 2021-01-29 08:00:33
问题 I'm implementing BST (Binary Search Tree), RBT(Red-Black Tree), and AVLT (AVL Tree). I wrote a BST as follows: use std::cell::RefCell; use std::rc::Rc; use std::cmp::max; type RcRefBaseNode<T> = Rc<RefCell<BaseNode<T>>>; type BaseNodeLink<T> = Option<RcRefBaseNode<T>>; struct BaseNode<T: Ord> { pub key: T, left: BaseNodeLink<T>, right: BaseNodeLink<T>, } struct BaseTree<T: Ord> {root: BaseNodeLink<T>} impl<T: Ord> BaseTree<T> { fn new(data: T) -> Self { Self{ root: Some(Rc::new(RefCell::new

How can I extend the lifetime of a temporary variable inside of an iterator adaptor in Rust?

雨燕双飞 提交于 2021-01-29 07:51:09
问题 I have a method make_iter() which creates an Iterator with multiple adapters in Rust, which can be simplified as the following MCVE: fn make_iter(first: &First) -> Box<dyn Iterator<Item = String> + '_> { Box::new(first.make_objects().flat_map(|second| { second .iter() .filter(|third| third.as_str() != "t2") .flat_map(|third| vec![format!("{}.A", third), format!("{}.B", third)].into_iter()) .chain( vec![ format!("{}.A", second.name()), format!("{}.B", second.name()), ] .into_iter(), ) })) }

Overflow evaluating the requirement when returning a recursive iterator using impl trait

孤人 提交于 2021-01-29 07:18:42
问题 I'm trying to iterate depth-first over a tree structure in Rust. I thought I had a really nice concise solution for this, but I can't get it to compile. Conceptually it's pretty simple: iterate over the children, get each child's depth first iterator, flatten them, and chain the current node's metadata iterator to it. #[derive(Debug, Eq, PartialEq)] struct Node { metadata: Vec<i64>, children: Vec<Box<Node>>, } impl Node { fn depth_first_metadata_iter(&self) -> impl Iterator<Item = &i64> + '_

How to flatten a `Vec` field when serializing a struct with serde?

谁都会走 提交于 2021-01-29 04:45:22
问题 I've got some XML that has a tag containing multiple sibling tags with the same name, like so: <foo> <bar/> <bar/> </foo> (There may also be multiple top level <foo> s as well, though I've not gotten around to trying to (de)serialize that yet.) Using this code: use serde::{Deserialize, Serialize}; use quick_xml::de::from_str; use quick_xml::se::to_string; #[derive(Debug, Deserialize, PartialEq, Serialize)] pub struct Foo { #[serde(rename = "bar", default)] bars: Vec<Bar>, } #[derive(Debug,

Is there a subtrait of `Index` that specifies the `len` method?

元气小坏坏 提交于 2021-01-29 03:59:59
问题 The std::ops::Index trait is implemented by types that support array subscript notation. It appears that most types that implement Index also have a len method, but it is not part of the trait so you can't assume it exists. Therefore, I find myself writing code specialized for slices (which do have a len method), but I would prefer to be more general. Is there a subtrait of Index that specifies the len method or in some other way reveals what range of indices is allowed? 回答1: Is there a

Array cannot be indexed by RangeFull?

两盒软妹~` 提交于 2021-01-29 03:52:56
问题 Consider the following example: use std::ops::Index; use std::ops::RangeFull; fn f<T: Index<RangeFull>>(x: T) {} fn main() { let x: [i32; 4] = [0, 1, 2, 3]; f(x); } Upon calling f(x) , I get an error: error[E0277]: the type `[i32; 4]` cannot be indexed by `std::ops::RangeFull` --> src/main.rs:8:5 | 8 | f(x); | ^ `[i32; 4]` cannot be indexed by `std::ops::RangeFull` | = help: the trait `std::ops::Index<std::ops::RangeFull>` is not implemented for `[i32; 4]` note: required by `f` --> src/main