rust

'inheritance' of generic trait implementation

一个人想着一个人 提交于 2021-01-28 19:50:59
问题 I wanted to try implementing a trait generically and have users of the trait inherit this 'base' implementation automatically as long as they are compatible. This is the test-code I came up with (note that fmt::Show is std::fmt::Show ): trait Outspoken { fn speak(&self) -> String; } impl<T: fmt::Show> Outspoken for T { fn speak(&self) -> String { format!("{:?}", self) } } // In theory, we can now let my-types speak #[derive(Show)] struct MyType(i32); // 'Show' works assert_eq!(format!("{:?}",

Reducing match indentation for deeply nested properties

天涯浪子 提交于 2021-01-28 18:29:50
问题 I need to refer to a value deep within a structure which includes an Option nested in a struct property, nested in a Result . My current (working) solution is: let raw = &packet[16..]; match PacketHeaders::from_ip_slice(raw) { Err(_value) => { /* ignore */ }, Ok(value) => { match value.ip { Some(Version4(header)) => { let key = format!("{}.{}.{}.{},{}.{}.{}.{}", header.source[0], header.source[1], header.source[2], header.source[3], header.destination[0], header.destination[1], header

Rust Inspect Iterator: cannot borrow `*` as immutable because it is also borrowed as mutable

試著忘記壹切 提交于 2021-01-28 18:27:13
问题 Why can't I push to this vector during inspect and do contains on it during skip_while ? I've implemented my own iterator for my own struct Chain like this: struct Chain { n: u32, } impl Chain { fn new(start: u32) -> Chain { Chain { n: start } } } impl Iterator for Chain { type Item = u32; fn next(&mut self) -> Option<u32> { self.n = digit_factorial_sum(self.n); Some(self.n) } } Now what I'd like to do it take while the iterator is producing unique values. So I'm inspect -ing the chain and

Is it possible to compile one specific library with the nightly compiler and link it to a project that is compiled on stable?

老子叫甜甜 提交于 2021-01-28 14:19:02
问题 I am working on a project where a dependency requires a specific nightly feature. I need to use this lib, but I am afraid that if I compile the project with nightly, I could depend on nother libraries that include another unstable feature dependency and I wouldn't be aware of that. Is it possible to compile the library that I need using nightly (while set the nightly version to version that already merged to the release branch) to some kind of "lib.a" file, and compile the whole project on

Is it possible to compile one specific library with the nightly compiler and link it to a project that is compiled on stable?

旧巷老猫 提交于 2021-01-28 14:17:05
问题 I am working on a project where a dependency requires a specific nightly feature. I need to use this lib, but I am afraid that if I compile the project with nightly, I could depend on nother libraries that include another unstable feature dependency and I wouldn't be aware of that. Is it possible to compile the library that I need using nightly (while set the nightly version to version that already merged to the release branch) to some kind of "lib.a" file, and compile the whole project on

String becomes empty passing through FFI from rust to ruby

烈酒焚心 提交于 2021-01-28 13:55:36
问题 I have a rubygem with a native extension written in rust. The native extension supports serializing its data-structure to JSON. However, whilst I've confirmed it's generating JSON, the string is always empty on the ruby side. Here's the ruby code: module RustCuckooFilter extend FFI::Library ffi_lib 'libcuckoofilter_cabi' class Instance < FFI::AutoPointer def export(path) RustCuckooFilter.export(self) end end attach_function :export, :rcf_cuckoofilter_export, [Instance], :pointer And the rust

Generic associated type may not live long enough

心已入冬 提交于 2021-01-28 13:50:44
问题 Take the following example (Playground): #![feature(generic_associated_types)] #![allow(incomplete_features)] trait Produce { type CustomError<'a>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>>; } struct GenericProduce<T> { val: T, } struct GenericError<'a, T> { producer: &'a T, } impl<T> Produce for GenericProduce<T> { type CustomError<'a> = GenericError<'a, T>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>> { Err(GenericError{producer: &self.val}) } } The

Generic associated type may not live long enough

喜欢而已 提交于 2021-01-28 13:39:07
问题 Take the following example (Playground): #![feature(generic_associated_types)] #![allow(incomplete_features)] trait Produce { type CustomError<'a>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>>; } struct GenericProduce<T> { val: T, } struct GenericError<'a, T> { producer: &'a T, } impl<T> Produce for GenericProduce<T> { type CustomError<'a> = GenericError<'a, T>; fn produce<'a>(&'a self) -> Result<(), Self::CustomError<'a>> { Err(GenericError{producer: &self.val}) } } The

Having a child modify its parent

早过忘川 提交于 2021-01-28 12:33:50
问题 I want to have a child struct modify its parent struct. Example: // Problem: Having a struct's field modify the struct in which the field is. // MUST NOT be copyable nor clonable struct Updatee { value: u64, updater: Updater } impl Updatee { fn update(&mut self) { // ERROR: cannot borrow `*self` as mutable more than once at a time self.updater.update(self); } } // MUST NOT be copyable nor clonable struct Updater { value: u64 } impl Updater { fn update(&mut self, updatee: &mut Updatee) { self

Having a child modify its parent

六眼飞鱼酱① 提交于 2021-01-28 12:31:07
问题 I want to have a child struct modify its parent struct. Example: // Problem: Having a struct's field modify the struct in which the field is. // MUST NOT be copyable nor clonable struct Updatee { value: u64, updater: Updater } impl Updatee { fn update(&mut self) { // ERROR: cannot borrow `*self` as mutable more than once at a time self.updater.update(self); } } // MUST NOT be copyable nor clonable struct Updater { value: u64 } impl Updater { fn update(&mut self, updatee: &mut Updatee) { self