rust


Is it possible to create a macro to implement builder pattern methods?


I have a builder pattern implemented for my struct:
pub struct Struct {
pub grand_finals_modifier: bool,
}
impl Struct {
pub fn new() -> Struct {
Struct {
grand_finals_modifier: false,
}
}
pub fn grand_finals_modifier<'a>(&'a mut self, name: bool) -> &'a mut Struct {
self.grand_finals_modifier = grand_finals_modifier;
self
}
}
Is it possible in Rust to make a macro for methods like this to generalize and avoid a lot of duplicating code? Something that we can use as the following:
impl Struct {
builder_field!(hello, bool);
}
After reading the documentation, I've come up with this code:
macro_rules! builder_field {
($field:ident, $field_type:ty) => {
pub fn $field<'a>(&'a mut self,
$field: $field_type) -> &'a mut Self {
self.$field = $field;
self
}
};
}
struct Struct {
pub hello: bool,
}
impl Struct {
builder_field!(hello, bool);
}
fn main() {
let mut s = Struct {
hello: false,
};
s.hello(true);
println!("Struct hello is: {}", s.hello);
}
It does exactly what I need: creates a public builder method with specified name, specified member and type.

Related Links

Is there a way to let Rust infer the correct type for an associated type?
How to encapsulate resources and their references in one struct in Rust? [duplicate]
How would one return a function from a function in Rust?
Lifetime on trait returning iterator
Mutable borrow of self doesn't change to immutable
How do I specify the linker path in Rust?
Are raw pointers to temporaries ok in Rust?
How to destructure tuple struct with reference
Unable to use std::process::Command to SSH - No such file or directory
“borrowed value does not live long enough” error in trie insertion
How to check for EOF in read_line in Rust 1.12?
Can I create an owned pointer to a stack object
How do I abort a Rust process?
Is it possible to disable Rust's lifetime elision?
How to implement Index over a wrapped HashMap?
Why is it allowed to pass the parameter value by reference with the duplicated sign &? [duplicate]

Categories

HOME
qt
magento2
angular-ui-grid
schema.org
workflow
scroll
consul
gallery
lvm
spring-amqp
sonata
google-tasks-api
vmware-workstation
susy-sass
psql
multiple-tables
amazon-mws
opam
altium-designer
eclipse-emf
exchange-server-2010
strophe.js
sap-lumira
jackrabbit-oak
onchange
ccavenue
bobo-browse.net
adapter
sales
splunk-query
d3.js-v4
dotcms
instructions
mapnik
fotorama
actframework
private-key
apptentive
xerces-c
assertions
pdfnet
ms-solver-foundation
django-smart-selects
llvm-ir
sspi
transparency
bit-shift
jenkins-slave
music21
adblock
spongycastle
cyanogenmod
android-5.0-lollipop
xcglogger
subforms
simple-schema
clrs
lvalue
degrees
adobe-reader
google-identity-toolkit
clp
coovachilli
xcode8-beta4
cdo.message
oci
node-glob
teamviewer
univocity
flush
ioctl
opencyc
nette
highest
libz
prism-4
shell-extensions
pass-by-value
directorysearcher
producer
angular-ui-select
rpg
distributed-r
lync-2010
strdup
mp4parser
stty
coalesce
background-thread
dojo-1.9
smartfox
user-forums
system-requirements
clearinterval
sfinae
uitextfielddelegate
kaazing
litespeed
differentiation
asio
servlet-container
mygeneration
yahoo-maps
powergui
subtext
tso
autobench
community-server

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App