Hi,
In a project I'm working on, I have a trait A
that defines an interface. Furthermore, I have a trait, let's call it ExportA
, that relies on A, which is used to export object implementing A
to a certain format. ExportA
contains a default implementation, which can be overridden by the user:
trait A { /* ... */ }
trait ExportA: A
{
fn export(&self) -> MyResult<String>
{
/* ... */
}
}
This works fine, and I can use the default functionality of ExportA
if I add an empty implementation of the trait:
struct MyA
{
/* ... */
}
impl A for MyA
{
/* ... */
}
impl ExportA for MyA {}
Now, what I would like, is that users of my crate don't have to add this empty implementation. In some cases the export functionality is not used, sometimes the default implementation just suffices, and having to add implementations for traits that aren't used just leads to a lot of unnecessary boiler plating. I can't add a default implementation for all objects implementing A
, like
impl<T: A> ExportA for T {}
because then the default implementation is always used (what /is/ the status of specialization BTW?). Another solution would be to move the functionality of ExportA
into A
proper, but I'd prefer not to that either, since the export really is orthogonal to what A
provides.
So, how would I go about it then? Any help would be greatly appreciated!