Below is my non-compiling code to see my situation.
I want to create a logger with a type that implements Debug
and Clone
. This is because I first want to log the error (I think I need to clone the value) on the machine, and then send it to the database.
I want to initialize/use my type with either:
&str
String
- Some type that implements
Debug
+Clone
Notes:
- I don't want specialized methods for each of the above types. I have a lot of methods and I don't want to keep repeating the generics.
- I don't want calling my type with a generic.
- I don't care about some extra allocation, a.k.a using a
Box
for example. - I will never assign the result of an instance of Logger to a variable (maybe this enables the type to be unsized?), I just log a few things.
Trying to Box
around the Debug
and Clone
requirement gives me this error...
only auto traits can be used as additional traits in a trait object
... when using something like this:
pub fn new<D: Clone + Debug>(con: &'a DBCon, err: D) -> Logger
Code
struct DBCon;
struct Logger<'a> {
con: &'a DBCon,
err: ?? // Some type implementing Debug + Clone
}
impl<'a> Logger<'a> {
pub fn new(con: &'a DBCon, err: ??) -> Logger {
Logger { con, err }
}
pub fn log(self) {
eprintln!("{:?}", self.err.clone())
// Send self.err to database
}
// Additional log methods
}
fn main() {
Logger::new(con: some_con, "err").log();
Logger::new(con: some_con, "err".to_owned()).log();
Logger::new(con: some_con, some_other_value_that_impl_Debug_and_Clone).log();
}