Hi,
In my project, I use something like:
struct Database {
db: Box<Store + 'static>,
logger: Box<Logger + 'static>,
}
Does anyone know what is the difference? They seem to work the same to me.
Philippe
On 12/01/2014 05:04 PM, Federico Ravasio wrote:
понедельник, 1 декабря 2014 г. в 16:59, Ryan Michael написал:
I'm curious if the community has some design approaches to encapsulating
interfaces to multiple generic implementations. For example, imagine I was
writing an abstract database interface with multiple backing implementations
for different stores (sqlite, pgsql, mysql, etc) and logging data to different
types of loggers (stdout, file, network-attached, etc). The goal would be to
implement some shared methods (insert, query, delete) to be delegated to
different stores, with logging about the results.
My first approach to this was to create traits, and use them in a struct
trait Logger {
fn log(&mut self, message: String),
}
trait Store {
fn insert(&mut self, value: String),
fn query(&mut self, conditions: String) -> Vec<String>,
fn delete(&mut self, value: String),
}
struct Database {
db: Store,
logger: Logger,
}
AFAICT, this isn't possible - struct fields seem to require concrete types.
Hello Ryan,
I’ve had some luck by wrapping trait fields into boxes, with explicit lifetimes.
This should work:
struct Database<'a> {
db: Box<Store + 'a>,
logger: Box<Logger + 'a>,
}
Now, I’m not sure if that’s a community-accepted solution. Let’s see what the
others think. :)
Cheers,
Federico
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev