Finding crates to do #[derive(ToOwned)] and #[derive(AsRef)]

I am create generic structs, each struct has a ToOwned and an AsRef implementation. But writing ToOwned implementation for every struct is tedious. It would be cool if there is a crate that lets me do something like this:

#[derive(ToOwned, AsRef)]
#[to_owned(from = "Foo<&str>", to = "Foo<String>")]
#[as_ref(from = "Foo<String>", to = "Foo<&str>")]
struct Foo<T: AsRef<str>> {
  pub a: T,
  pub b: Bar<T>,

That's not how ToOwned and AsRef work?

AsRef is a by-reference conversion: &T&U. It can't return a non-reference, or a reference to something that isn't borrowed from the original T.

ToOwned is the inverse of Borrow, which has the same limitations as AsRef (plus additional constraints on Eq and Hash).

Update: It looks like derive_more supports AsRef. I'm not sure how you could derive ToOwned or Borrow in a way that would actually work.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.