rust

What is the standard way to call a mutable method in a Rc-wrapped object?

穿精又带淫゛_ 提交于 2020-08-27 07:10:07
问题 In the following code, I am trying to change the value of a refcounted object by calling one of its methods: use std::rc::Rc; fn main() { let mut x = Rc::new(Thing { num: 50 }); x.what_to_do_to_get_mut_thing().change_num(19); //what do i do here } pub struct Thing { pub num: u32, } impl Thing { pub fn change_num(&mut self, newnum: u32) { self.num = newnum; } } I am using the get_mut function to achieve this, but I don't know if this is a standard way to accomplish this. if let Some(val) = Rc:

Deriving Serde's Serialize or Deserialize forces generic type to be serialisable although it does not need to be

天涯浪子 提交于 2020-08-27 06:43:54
问题 My type A , which can contain anything that implements trait Trait , is serialisable, although the type implementing the trait Trait might not be. In my case, it cannot be - it's a private asymmetric key: extern crate serde; #[macro_use] extern crate serde_derive; use serde::de::DeserializeOwned; use serde::Serialize; trait Trait { type SerialisableType: Clone + Serialize + DeserializeOwned; fn inner(&self) -> &Self::SerialisableType; } #[derive(Serialize, Deserialize)] enum A<T: Trait> {

Deriving Serde's Serialize or Deserialize forces generic type to be serialisable although it does not need to be

耗尽温柔 提交于 2020-08-27 06:42:31
问题 My type A , which can contain anything that implements trait Trait , is serialisable, although the type implementing the trait Trait might not be. In my case, it cannot be - it's a private asymmetric key: extern crate serde; #[macro_use] extern crate serde_derive; use serde::de::DeserializeOwned; use serde::Serialize; trait Trait { type SerialisableType: Clone + Serialize + DeserializeOwned; fn inner(&self) -> &Self::SerialisableType; } #[derive(Serialize, Deserialize)] enum A<T: Trait> {

Deriving Serde's Serialize or Deserialize forces generic type to be serialisable although it does not need to be

非 Y 不嫁゛ 提交于 2020-08-27 06:42:02
问题 My type A , which can contain anything that implements trait Trait , is serialisable, although the type implementing the trait Trait might not be. In my case, it cannot be - it's a private asymmetric key: extern crate serde; #[macro_use] extern crate serde_derive; use serde::de::DeserializeOwned; use serde::Serialize; trait Trait { type SerialisableType: Clone + Serialize + DeserializeOwned; fn inner(&self) -> &Self::SerialisableType; } #[derive(Serialize, Deserialize)] enum A<T: Trait> {

Why define a struct with single private field of unit type?

浪子不回头ぞ 提交于 2020-08-26 14:01:23
问题 I do not know why struct ParseBoolError has the _priv field, as shown below: pub struct ParseBoolError { _priv: (), } https://doc.rust-lang.org/src/core/str/mod.rs.html#150 I think that the _priv field is not used. 回答1: You can't create an instance of a struct if it has private fields. This is just a trick to prevent ParseBoolError from being constructed in user code. One reason to do this is for forwards compatibility. If users could create it with: let error = ParseBoolError {}; then a

Confusing automatic dereferencing of Arc

折月煮酒 提交于 2020-08-25 08:20:12
问题 This is an example taken from the Mutex documentation: use std::sync::{Arc, Mutex}; use std::sync::mpsc::channel; use std::thread; const N: usize = 10; fn main() { let data = Arc::new(Mutex::new(0)); let (tx,rx) = channel(); for _ in 0..N{ let (data, tx) = (data.clone(), tx.clone()); thread::spawn(move || { // snippet }); } rx.recv().unwrap(); } My question is where the snippet comment is. It is given as let mut data = data.lock().unwrap(); *data += 1; if *data == N { tx.send(()).unwrap(); }

I Can't Get Cross Compiling From Ubuntu To Windows Working

為{幸葍}努か 提交于 2020-08-25 07:27:21
问题 I Want To Cross Compile Some Rust Code from Ubuntu to Windows, and receive an error about onexitbegin. Tried to follow various suggestions, but they don't have my specific error message: crt2.o:crtexe.c: (.rdata$.refptr.__onexitend[.refptr.__onexitend]+0x0): undefined reference to `__onexitend' collect2: error: ld returned 1 exit status cargo build --release --target x86_64-pc-windows-gnu Expected to get something built, but it blows up. The output says this: /usr/bin/x86_64-w64-mingw32-ld:

I Can't Get Cross Compiling From Ubuntu To Windows Working

北城余情 提交于 2020-08-25 07:27:08
问题 I Want To Cross Compile Some Rust Code from Ubuntu to Windows, and receive an error about onexitbegin. Tried to follow various suggestions, but they don't have my specific error message: crt2.o:crtexe.c: (.rdata$.refptr.__onexitend[.refptr.__onexitend]+0x0): undefined reference to `__onexitend' collect2: error: ld returned 1 exit status cargo build --release --target x86_64-pc-windows-gnu Expected to get something built, but it blows up. The output says this: /usr/bin/x86_64-w64-mingw32-ld:

How to implement a `Future` / `Stream` that polls `async fn(&mut self)`?

↘锁芯ラ 提交于 2020-08-24 11:25:01
问题 I have the following struct struct Test; impl Test { async fn function(&mut self) {} } I want to implement an std::future::Future (well, actually futures::Stream , but it's basically the same) on Test , that would poll the function . My first try looked something like this impl Future for Test { type Output = (); fn poll(self: Pin<&mut self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.function() { Poll::Pending => Poll::Pending, Poll::Ready(_) => Poll::Ready(()), } } } Obviously

How to implement a `Future` / `Stream` that polls `async fn(&mut self)`?

安稳与你 提交于 2020-08-24 11:24:41
问题 I have the following struct struct Test; impl Test { async fn function(&mut self) {} } I want to implement an std::future::Future (well, actually futures::Stream , but it's basically the same) on Test , that would poll the function . My first try looked something like this impl Future for Test { type Output = (); fn poll(self: Pin<&mut self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.function() { Poll::Pending => Poll::Pending, Poll::Ready(_) => Poll::Ready(()), } } } Obviously