emk / accessors

(WIP) Getters and setters for Rust using macros 1.1
11 stars 6 forks source link

Support overriding the return type of a getter for deref-coercible types #3

Open Arnavion opened 7 years ago

Arnavion commented 7 years ago

For example, to allow a getter for a foo: Foo field to return &Bar instead of &Foo. For example a #[getter(return = "&Bar")] field attribute. Of course this will only work for deref-coercible types since the body of the getter will still be &self.foo.


It might also be good to have some overrides like this as part of the base crate. For example, my own macro has overrides for String -> &str, Option<String> -> Option<&str>, Option<T> -> Option<&T>, Vec<String> -> Vec<&str>, etc.

emk commented 7 years ago

I would definitely be happy to support better defaults for common return types. Some possibilities:

…where Recurse! means "apply this rule recursively on the contained type". The trick is dealing with Copy versus !Copy. I wonder if there's a trait somewhere in the standard lib which allows us to distinguish between these cases?