emk / accessors Goto Github PK
View Code? Open in Web Editor NEW(WIP) Getters and setters for Rust using macros 1.1
(WIP) Getters and setters for Rust using macros 1.1
Emit #[allow(missing_docs)]
on the impl block.
Reuse doc comments on the fields for the corresponding getters and setters. The comments won't be exactly in the style that getters should have and even worse for setters, but it may be better than nothing. My getters macro does this.
You could also support overriding the doc comment with a field-level attribute like #[getter(doc = "Gets the foo.")]
#[setter(doc = "Sets the foo.")]
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.
For example, via a #[getter(ignore)]
field attribute.
Should be as simple as leaving #ast
out of the resulting TokenStream.
When I tried to compile 'simple.rs' the following error appeared:
error: setters
is a derive mode
--> src\main.rs:7:1
|
7 | #[setters(into)]
| ^^^^^^^^^^^^^^^^
When I commented out '#[setters(into)]' string the other error appeared:
error[E0428]: a type named Simple
has already been defined in this module
--> src\main.rs:8:1
|
6 | #[derive(getters, setters)]
| ------- previous definition of Simple
here
7 | //#[setters(into)]
8 | / struct Simple {
9 | | field: String,
10 | | }
| |_^ Simple
already defined
error[E0308]: mismatched types
--> src\main.rs:15:17
|
15 | s.set_field("there");
| ^^^^^^^ expected struct std::string::String
, found reference
|
= note: expected type std::string::String
found type &'static str
= help: here are some functions which might fulfill your needs:
- .escape_debug()
- .escape_default()
- .escape_unicode()
- .to_lowercase()
- .to_uppercase()
error: aborting due to previous error(s)
accessors-test.zip
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.