rust


Traits with associated type in templates


I have been having problems compiling my Rust code, I managed to boil down the issue to this snippet:
use std::slice::Iter;
pub trait Foo<'a> {
type Bar: Iterator<Item=&'a usize>;
fn make(&self) -> usize;
}
pub struct Juice;
impl <'a> Foo<'a> for Juice {
type Bar = Iter<'a, usize>;
fn make(&self) -> usize { 0us }
}
// Uncomment this line to break things
// fn get_int<'a, T: Foo<'a>>(t: T) -> usize {
// t.make()
// }
fn main() {
println!("Hello, {:?} world!" , Juice.make());
}
I am pretty sure I am just missing something, is there anything I need to do to make this trait work? I am using the latest nightly alpha build (at the time of writing):
rustc 1.0.0-nightly (458a6a2f6 2015-01-25 21:20:37 +0000)
Unfortunately, you need to write this:
fn get_int<'a, T: Foo<'a, Bar=I>, I: Iterator<Item=&'a usize>>(t: T) -> usize {
t.make()
}
That is, you have to specify explicitly that the type of Bar is an iterator of the corresponding type. The trait bound inside the trait definition alone is insufficient.
This is very much like regular type parameters work. Even if you write something like
trait Parameterized<T: Clone> { ... }
You still need to write
fn do_something<P: Parameterized<T>, T: Clone>() { ... }
Or with structs:
struct S<T: Iterator<i32>> { ... }
impl<T: Iterator<i32>> for S<T> { ... }
This does look counterintuitive (and I've stumbled upon this several times as well) and probably deserves an issue in RFC repo.

Related Links

Using loop variable from “..” loop causes type conversion?
What is RFC 401's coerce_inner useful for?
Rust Borrow checker only complains about borrowing as mutable multiple times when a function that returns a reference with the same lifetime assigned
How can I remove `let _ : () = …`?
Does if-let with a pair short-circuit?
Rust borrow lasts beyond the scope it's in?
How can I include an arbitrary set of Protobuf-built files without knowing their names?
Specialization of method in inherent impl
Getting reference to object behind Rc
Similar implementations of one trait for many structs
Unable to find unsafe trait implementation
Injecting a Diesel connection into an Iron middleware
Rust function does not have static lifetime?
Can Rust optimise away the bit-wise copy during move of an object someday?
Jack audio client name longer than 4 characters breaks client
What is the difference between using a type as a different name and a type alias?

Categories

HOME
hpoo
webview
knockout.js
mod-rewrite
yaml
shinyapps
softlayer
selenium-builder
foaf
theano
initialization
twitter-fabric
gallery
qooxdoo
timber
jpa-criteria
mutation-testing
log4j2
android-externalstorage
jpa-2.0
orange-api
boomi
deepfreeze
infrared
adminlte
spyder
coreldraw
sharedpreferences
altium-designer
nurbs
introspection
materialize
matlab-app-designer
instructions
nsmutablearray
john-the-ripper
microsoft-certifications
gsp
listjs
code-rally
mv
ng-repeat
utf
get-event-store
oauth2-playground
angular-datatables
morphline
jenkins-slave
axis
qvtkwidget
viewmodel
solr-query-syntax
visual-format-language
line-intersection
mura
winmerge
google-news
pecl
emf-compare
nrf51
pbx
dojo-build
right-to-left
strpos
ioctl
python-cffi
juniper-network-connect
android-studio-import
supercomputers
dynamics-ax-2012-r2
abstract-factory
quercus
scala-swing
fiware-bosun
mojo
device-admin
producer
osascript
box2dweb
maven-central
lsa
armv6
jmenubar
hyphen
infinite
mute
sql-view
jquery-transit
pacman
notin
lynx
xalan
arrow
getopt-long
viewpagerindicator
jzmq
gamesalad
nsfont
microblogging
background-repeat
litespeed
mongrel2
suphp
formal-semantics
icicles
reliability
suggestbox
activex-exe
gui-designer
parentid

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App