sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.49k stars 80 forks source link

make error clonable #132

Open clouds56 opened 2 years ago

sfackler commented 2 years ago

What's the rationale for this? Error types are generally not cloneable.

clouds56 commented 2 years ago

Thanks for fast reply! Error type that not cloneable usually has a cloneable ErrorKind like std::io::ErrorKind. The error here could derive clone, so why not?

sfackler commented 2 years ago

But how do you concretely want to use the clonability of those things?

clouds56 commented 2 years ago

I'm using thiserror wrap errors, if r2d2::Error supports clone, I could use #[from]. The error would be cloned and send to log thread so it must derive Clone

#[derive(Debug, Clone, thiserror::Error)]
pub enum Error {
  #[error("pool error: {0}")]
  PoolArc(Arc<r2d2::Error>),
  #[error("pool error: {0}")]
  Pool(#[from] r2d2::Error),
}

You could see lots of error's (if not most) in std derives clone even they could have a large Vec<u8>, FromVecWithNulError, FromUtf8Error, fmt::Error, VarError, SystemTimeError, I think the policy is just derive Clone whenever possible. The std::io::Error is an exception since it couldn't.