Skip to content

Instantly share code, notes, and snippets.

@jimmycuadra
Last active December 21, 2015 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmycuadra/c00813610a3aab67fca7 to your computer and use it in GitHub Desktop.
Save jimmycuadra/c00813610a3aab67fca7 to your computer and use it in GitHub Desktop.
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