traits

Is it possible to specialize on a static lifetime?

断了今生、忘了曾经 提交于 2019-12-01 20:26:12
问题 I want to specialize &'static str from &'a str . Something like this: use std::borrow::Cow; struct MyString { inner: Cow<'static, str>, } impl From<&'static str> for MyString { fn from(x: &'static str) -> Self { MyString { inner: Cow::Borrowed(x), } } } impl<T: Into<String>> From<T> for MyString { fn from(x: T) -> Self { MyString { inner: Cow::Owned(x.into()), } } } fn main() { match MyString::from("foo").inner { Cow::Borrowed(..) => (), _ => { panic!(); } } let s = String::from("bar"); match

Why are supertrait bounds other than the first not recognized on an associated type?

孤街醉人 提交于 2019-12-01 20:03:23
This snippet is valid in Rust 1.26.1: use std::ops::AddAssign; trait Trait where for<'a> Self: AddAssign<Self> + AddAssign<&'a Self> + Sized, { } trait Trait2 { type Associated: Trait; fn method(u32) -> Self::Associated; } fn func<T2: Trait2>() { let mut t = T2::method(1); let t2 = T2::method(2); t += &t2; } Notice that Trait implements both AddAssign<Self> and AddAssign<&'a Trait> (in that order, which is important later). Therefore, in func we know that both t += t2 and t += &t2 should be valid. As seen on the playground , t += &t2 is valid, but using t += t2 isn't : error[E0308]: mismatched

Why are supertrait bounds other than the first not recognized on an associated type?

女生的网名这么多〃 提交于 2019-12-01 19:41:09
问题 This snippet is valid in Rust 1.26.1: use std::ops::AddAssign; trait Trait where for<'a> Self: AddAssign<Self> + AddAssign<&'a Self> + Sized, { } trait Trait2 { type Associated: Trait; fn method(u32) -> Self::Associated; } fn func<T2: Trait2>() { let mut t = T2::method(1); let t2 = T2::method(2); t += &t2; } Notice that Trait implements both AddAssign<Self> and AddAssign<&'a Trait> (in that order, which is important later). Therefore, in func we know that both t += t2 and t += &t2 should be

core::marker::Sized not implemented for Foo

不羁岁月 提交于 2019-12-01 17:07:00
问题 I have this fairly straightforward Rust program: use std::ops::Deref; trait Foo { fn foo(&self); } impl Foo for () { fn foo(&self) { println!("hello world"); } } impl<F> Foo for Box<F> where F: Foo { fn foo(&self) { let f: &F = self.deref(); f.foo() } } fn call_foo<F>(foo: &F) where F: Foo { foo.foo() } fn main() { let foo: Box<Foo> = Box::new(()); call_foo(&foo); } But I get a compilation errors: $ rustc main.rs main.rs:26:3: 26:11 error: the trait `core::marker::Sized` is not implemented

Trait `x` is not implemented for the type `x`

风流意气都作罢 提交于 2019-12-01 16:55:24
When compiling the following code: trait RenderTarget {} struct RenderWindow; impl RenderTarget for RenderWindow {} trait Drawable { fn draw<RT: RenderTarget>(&self, target: &mut RT); } fn main() { let mut win = RenderWindow; let mut vec: Vec<Box<Drawable>> = Vec::new(); for e in &vec { e.draw(&mut win); } } I get the error: error: the trait `Drawable` is not implemented for the type `Drawable` [E0277] src/main.rs:15 e.draw(&mut win); ^~~~~~~~~~~~~~ What is the error message trying to tell? Also, how to fix it? There's a related question but the solution there was to modify the trait A (which

Why does Rust not allow coercion to trait objects inside containers?

坚强是说给别人听的谎言 提交于 2019-12-01 16:15:25
I have a Vec<Box<T>> where T implements Foo . Why can I not coerce it to a Vec<Box<Foo>> even though I can coerce anything of type Box<T> into a Box<Foo> ? Why does the below code not compile? use std::vec; trait Foo {} struct Bar {} impl Foo for Bar {} fn main() { let v = vec![Box::new(Bar {})]; let v_1 = v as Vec<Box<Foo>>; } Because Box<Bar> is a different size than Box<Foo> . The coercion is allowed on a single value, but here you'd have to resize the whole vector. The book goes into some detail on this in the section on Representation of Trait Objects . Short version: Box<Bar> is a

How can I use the default implementation of a trait method instead of the type's custom implementation?

[亡魂溺海] 提交于 2019-12-01 15:57:54
问题 Some trait methods have default implementations which can be overwritten by an implementer. How can I use the default implementation for a struct that overwrites the default? For example: trait SomeTrait { fn get_num(&self) -> i32; fn add_to_num(&self) -> i32 { self.get_num() + 1 } } struct SomeStruct; impl SomeTrait for SomeStruct { fn get_num(&self) -> i32 { 3 } fn add_to_num(&self) -> i32 { self.get_num() + 2 } } fn main() { let the_struct = SomeStruct; println!("{}", the_struct.add_to_num

Mismatch between associated type and type parameter only when impl is marked `default`

限于喜欢 提交于 2019-12-01 15:51:50
问题 The following code results in an error (Playground) #![feature(specialization)] trait Foo { type Assoc; fn foo(&self) -> &Self::Assoc; } default impl<T> Foo for T { type Assoc = T; fn foo(&self) -> &Self::Assoc { self } } Error: error[E0308]: mismatched types --> src/main.rs:20:9 | 20 | self | ^^^^ expected associated type, found type parameter | = note: expected type `&<T as Foo>::Assoc` found type `&T` This is strange since <T as Foo>::Assoc is T , so it should work. Stranger even: when I

Implementing a trait for closures results in bound/concrete lifetime mismatch

梦想与她 提交于 2019-12-01 15:09:34
问题 I want to implement a trait for closures of a specific type. Here is a minimal example (playground): trait Foo { fn foo(&self, x: &u32); } impl<F> Foo for F where F: Fn(&u32) { fn foo(&self, x: &u32) { self(x) } } fn main() { let _: &FnOnce(&u32) = &|x| {}; // works let _: &Foo = &|x| {}; // doesn't work } It results in this error: error: type mismatch resolving `for<'r> <[closure@<anon>:16:29: 16:35] as std::ops::FnOnce<(&'r u32,)>>::Output == ()`: expected bound lifetime parameter , found

How do I make a struct callable?

喜欢而已 提交于 2019-12-01 14:24:47
问题 #![feature(unboxed_closures)] #![feature(fn_traits)] struct foo; impl std::ops::Add for foo { type Output = foo; fn add(self, x: foo) -> foo { println!("Add for foo"); x } } impl Fn for foo { extern "rust-call" fn call(&self) -> Self { println!("Call for Foo "); self } } fn main() { let x = foo; let y = foo; x + y; x(); } I implemented the Add trait, but I don't understand how to call the struct as a function. I get the error: error[E0243]: wrong number of type arguments: expected 1, found 0