Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Clonable errors #121

Closed
jugglerchris opened this issue Feb 4, 2017 · 8 comments
Closed

Clonable errors #121

jugglerchris opened this issue Feb 4, 2017 · 8 comments

Comments

@jugglerchris
Copy link

I have an application where it would be handy to be able to clone the Error type declared byerror_chain!(). Is there (or could there be) a way to add a #[derive(Clone)]?

@Yamakaky
Copy link
Contributor

Yamakaky commented Feb 4, 2017

Hum, we have the same thing for Sync, Eq... Maybe allow the user to declare the exact types he wants? What do you think?

@jugglerchris
Copy link
Author

Yes, that sounds useful.

@Yamakaky
Copy link
Contributor

Yamakaky commented Feb 6, 2017

Idea about the syntax changes? Maybe adding a new block type like foreign_errors or links, with a default to Sync + Send so that it's backward compatible.

@jugglerchris
Copy link
Author

My first thought would be allowing an actual #[derive(Clone,Sync)] before the errors { ... } block; I haven't looked into how easy (if at all) that would be to implement in the macro. A new block type would fit in with other error-chain syntax though, so I'm not really fussed either way personally. :-)

@Yamakaky
Copy link
Contributor

Yamakaky commented Feb 8, 2017

I starting looking about how to make the change. There is two troubles:

  1. State is crate-level, so we can't modify it for each error. We could make it generic but
  2. Rust doesn't allow us to do things like Box<Error + Debug>, Sync and Send being special cases.
    I think we have to generate an empty trait per Error, add the desired traits as dependencies and implement it for each struct that implements all the dependencies. Something like
trait Trait: Debug + Sync + Clone {}
impl<T: Debug + Sync + Clone> Trait for T {}

What do you think?

@Yamakaky
Copy link
Contributor

Yamakaky commented Feb 8, 2017

Apparently, Clone is not object safe, so we can't add it as a dependency like above. The rest works though: https://play.rust-lang.org/?gist=7c05d66e1e017248fb61162b4c9aea92&version=stable&backtrace=1

@jugglerchris
Copy link
Author

I discovered the Clone problem when trying to implement it manually as a workaround. I guess I'll have to think again!

@Yamakaky
Copy link
Contributor

Closing for #134

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants