Explanation of Impl'ing types and Iron's TypeMap?

I've stumbled upon a confusing corner of Rust and am hoping somebody more experienced can help me out here. I am trying to implement persistent state (with the end goal of a user system) for my iron-based site. I found the crate persistent, which is a middleware-oriented abstraction layer over some basic Mutex/Arc/RwLock stuff.

In order to construct a new State using State::one (as seen here), my state's value must implement Key from iron::typemap::Key. This is an approximation of how I accomplished that:

struct Database {
      data: Vec<Data>

struct Data { 
      foo: String,
      bar: String

impl Key for Database {
    type Value = Database;

impl Key for Data {
    type Value = Data;

I am confused about what I'm doing here and why this is necessary. What exactly am I doing with respect to the type system here, and why can't these things infer their own types? Is there ever a use case where Value would not be the same as Key?


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.