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

Rust RAM limited LRU cache? [closed]
How to determine messages in .rs files generated from rust-protobuf included by include! macro
Replacing a borrowed Arc<RwLock>
How to concatenate a literal string with a const string?
Replacing a borrowed variable
Type hinting on Rust function calls
Nesting an iterator's loops
Do literal integral values have a specific type in Rust?
What value does the variable in the following code snippet have?
How do you import macros in submodules in Rust?
Local let like in caml
E0309: Constraining a generic type parameter's lifetime
Are nested matches a bad practice in idiomatic Rust?
Extern crate inside main method; module::Type vs main::module::Type
Iterating over named regex groups in Rust
How do I give a mutable reference to a builder but only an immutable reference to the built object?

Categories

HOME
google-chrome
powerbi
mod-rewrite
freeradius
yii
livecode
writefile
swi-prolog
operating-system
android-emulator
consul
tabs
dronekit-python
dependencies
openfoam
u-boot
minecraft
hspi
yahoo
google-tasks-api
reselect
android-5.1.1-lollipop
playframework-2.0
pickle
string-formatting
spring-ldap
activeadmin
backtracking
data-collection
unrar
beego
basex
instructions
publishing
memory-fences
delayed-job
rust-cargo
rspec-rails
lxml
android-collapsingtoolbar
ms-media-foundation
serversocket
ms-dos
elastica
ng-repeat
llvm-ir
linq.js
assert
spring-restcontroller
oauth2
microkernel
icepdf
virtual-memory
morphline
dredd
windows-media-player
directwrite
sonicmq
extjs4.1
dtd
dymola
wildfly-9
petsc
mifos
pjax
java-2d
okio
logical
system.net.mail
coovachilli
xcode8-beta4
ajaxmin
persistent-object-store
wikimedia-commons
bayesglm
android-mapview
place
bjam
search-form
angular-amd
phpredis
shell-extensions
subfolder
maven-shade-plugin
google-hangouts
lifetime
full-text-indexing
squeezebox
otl
stripe.net
fileshare
air-native-extension
cpu-time
map
lsa
application-blocks
revmob
insertonsubmit
acceptance-testing
pacman
dibs
mongo-jackson-mapper
tfs-sdk
reentrancy
cmath
gprof
liveconnect
nsfont
wcf-callbacks
datacontracts
canonicalization
nsconnection
webresponse
mongrel2
gtktextview
gui-designer
jvm-bytecode
error-detection
account-management

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App