rust

Is there a safer way to use unions to convert between integer and floating-point numbers?

对着背影说爱祢 提交于 2021-01-28 03:43:33
问题 I´m writing a VM in Rust and I have a C and C++ background. I need union-like functionality because on the VM stack I can either store an int or a float . In C I had a union: union stack_record_t { int i; float f; }; I can use the record as int or as float with zero runtime overhead. I have a static bytecode analyzer which will find type errors before the bytecode executes, so I don't have to store a flag alongside the record. I don´t know if it is a good idea to use unions in Rust because

Is undefined behavior possible in safe Rust?

我怕爱的太早我们不能终老 提交于 2021-01-28 03:21:26
问题 Is there any way to achieve undefined behavior in Rust without using unsafe ? Of course, such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're using only the standard one. 回答1: Absolutely, but any such case is a bug with Rust or the standard libary. My favorite example is LLVM loop optimization can make safe programs crash, which actually occurs due to a poor interaction of Rust and LLVM semantics: pub fn oops() { (|| loop { drop(42) })() } Compiled

How to advance through data from the std::io::Read trait when Seek isn't implemented?

一笑奈何 提交于 2021-01-28 03:05:04
问题 What's the best way to read from a type implementing the std::io::Read trait when the contents of the output isn't important? Possible options I see are: Read single bytes in a loop. Allocate a potentially huge vector and read into that. Something in-between... read into a fixed sized buffer in a loop . The first 2 options don't seem ideal, the third is OK but inconvenient. Does Rust provide a convenient way to achieve this? 回答1: You can use io::copy(), Read::take() and io::sink() to discard

Do I need to use a `let` binding to create a longer lived value?

本秂侑毒 提交于 2021-01-28 01:54:39
问题 I've very recently started studying Rust, and while working on a test program, I wrote this method: pub fn add_transition(&mut self, start_state: u32, end_state: u32) -> Result<bool, std::io::Error> { let mut m: Vec<Page>; let pages: &mut Vec<Page> = match self.page_cache.get_mut(&start_state) { Some(p) => p, None => { m = self.index.get_pages(start_state, &self.file)?; &mut m } }; // omitted code that mutates pages // ... Ok(true) } it does work as expected, but I'm not convinced about the m

How to advance through data from the std::io::Read trait when Seek isn't implemented?

微笑、不失礼 提交于 2021-01-28 00:49:59
问题 What's the best way to read from a type implementing the std::io::Read trait when the contents of the output isn't important? Possible options I see are: Read single bytes in a loop. Allocate a potentially huge vector and read into that. Something in-between... read into a fixed sized buffer in a loop . The first 2 options don't seem ideal, the third is OK but inconvenient. Does Rust provide a convenient way to achieve this? 回答1: You can use io::copy(), Read::take() and io::sink() to discard

Unable to compile Rust with no_std/lang_items

点点圈 提交于 2021-01-27 23:26:30
问题 I am attempting to set up a project very similar to dueboot. That is, Rust on embedded ARM. Right now, I'm only up to the point of compiling the Rust code, but I can't get it to compile. I've basically copied the rust code exactly from that project, but I don't fully understand the lang_items feature. #![feature(asm)] #![feature(lang_items)] #![feature(no_std)] #![no_std] use arduino::{init, delay, pinMode, digitalWrite, analogWrite, LOW, HIGH, OUTPUT}; mod arduino; #[lang="sized"] trait

What is the fastest correct way to detect that there are no duplicates in a JSON array?

 ̄綄美尐妖づ 提交于 2021-01-27 22:19:21
问题 I need to check if all items are unique in an array of serde_json::Value . Since this type does not implement Hash I came up with the following solution: use serde_json::{json, Value}; use std::collections::HashSet; fn is_unique(items: &[Value]) -> bool { let mut seen = HashSet::with_capacity(items.len()); for item in items.iter() { if !seen.insert(item.to_string()) { return false; } } true } fn main() { let value1 = json!([1, 2]); assert!(is_unique(&value1.as_array().unwrap())); let value2 =

Unable to use std::process::Command - No such file or directory

会有一股神秘感。 提交于 2021-01-27 21:50:13
问题 I am trying to use the following Rust code to connect to EC2 instances. #[test] fn client_ssh_timeout2() { match Command::new("/usr/bin/ssh -i /tmp/.ssh/25.pem ubuntu@ip").spawn() { Ok(_) => println!("Able to ssh"), Err(e) => println!("{:?}", e), }; } But I am getting the following error Error { repr: Os { code: 2, message: "No such file or directory" } } Has anyone been able to use std::process::Command or any other Rust library to connect to EC2 instances using PEM files? I tried using ssh2

How to borrow a field for serialization but create it during deserialization?

自闭症网瘾萝莉.ら 提交于 2021-01-27 19:27:02
问题 I have a struct like this: #[derive(Serialize, Deserialize)] struct Thing { pub small_header: Header, pub big_body: Body, } I want to serialize this Thing to send over the network. I already have a Body available but I can't move it (imagine I am doing something with it, and every now and then I receive a command to temporarily stop what I'm doing and send over whatever data I have now) and I can't copy it (it's too big, possibly hundreds of megabytes). So I'd like Serde to just borrow the

Use Trait as Vec Type

喜你入骨 提交于 2021-01-27 19:04:44
问题 I'm new to Rust and have seen some examples of people using Box to allow pushing many types that implement a certain Trait onto a Vec. When using a Trait with Generics, I have run into an issue. error[E0038]: the trait `collision::collision_detection::Collidable` cannot be made into an object --> src/collision/collision_detection.rs:19:5 | 19 | collidables: Vec<Box<Collidable<P, M>>>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `collision::collision_detection::Collidable`