Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Lifetime parameters on an associated typed
extern crate typemap;
use typemap::Key;
struct Repository<'a, 'b> {
one: SomeTypeThatHoldsAReference<'a>,
two: SomeTypeThatHoldsAReference<'b>,
}
struct RepositoryState;
/// What's the correct syntax for specifying lifetime parameters
/// on an associated type in a trait implementation? Shown below
/// are the things I tried and the compiler errors each generated.
// error: wrong number of lifetime parameters: expected 2, found 0 [E0107]
// type Value = Repository;
impl Key for RepositoryState {
type Value = Repository;
}
// error: use of undeclared lifetime name `'a` [E0261]
// type Value = Repository<'a, 'b>;
// error: use of undeclared lifetime name `'b` [E0261]
// type Value = Repository<'a, 'b>;
impl Key for RepositoryState {
type Value = Repository<'a, 'b>;
}
// error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207]
// impl<'a, 'b> Key for RepositoryState {
// error: the lifetime parameter `'b` is not constrained by the impl trait, self type, or predicates [E0207]
// impl<'a, 'b> Key for RepositoryState {
impl<'a, 'b> Key for RepositoryState {
type Value = Repository<'a, 'b>;
}
// error: ambiguous associated type; specify the type using the syntax `<Type as typemap::Key>::Value` [E0223]
// impl<'a, 'b> Key for RepositoryState where Key::Value: 'a + 'b {
// error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207]
// impl<'a, 'b> Key for RepositoryState where Key::Value: 'a + 'b {
// error: the lifetime parameter `'b` is not constrained by the impl trait, self type, or predicates [E0207]
// impl<'a, 'b> Key for RepositoryState where Key::Value: 'a + 'b {
impl<'a, 'b> Key for RepositoryState where Key::Value: 'a + 'b {
type Value = Repository<'a, 'b>;
}
// error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates [E0207]
// impl<'a, 'b> Key for RepositoryState where <RepositoryState as Key>::Value: 'a + 'b {
// error: the lifetime parameter `'b` is not constrained by the impl trait, self type, or predicates [E0207]
// impl<'a, 'b> Key for RepositoryState where <RepositoryState as Key>::Value: 'a + 'b {
impl<'a, 'b> Key for RepositoryState where <RepositoryState as Key>::Value: 'a + 'b {
type Value = Repository<'a, 'b>;
}
// error: use of `Self` outside of an impl or trait [E0411]
// impl<'a, 'b> Key for RepositoryState where <Self as Key>::Value: 'a + 'b {
impl<'a, 'b> Key for RepositoryState where <Self as Key>::Value: 'a + 'b {
type Value = Repository<'a, 'b>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment