rust


Having a child modify its parent


I want to have a child struct modify its parent struct.
Example:
// Problem: Having a struct's field modify the struct in which the field is.
// MUST NOT be copyable nor clonable
struct Updatee {
value: u64,
updater: Updater
}
impl Updatee {
fn update(&mut self) {
// ERROR: cannot borrow `*self` as mutable more than once at a time
self.updater.update(self);
}
}
// MUST NOT be copyable nor clonable
struct Updater {
value: u64
}
impl Updater {
fn update(&mut self, updatee: &mut Updatee) {
self.value = self.value + 1;
updatee.value = self.value;
}
}
fn main() {
let updater = Updater { value: 0 };
let updatee = Updatee { value: 0, updater: updater };
updatee.update();
updatee.update();
assert_eq!(2, updatee.value);
}
I'm sure it can be done with unsafe blocks, but is there an alternative?
Perhaps my code is not idiomatic?
The reason you are getting the "already borrowed" error is because you are trying to pass the Updater to its update method twice, once as self, and once embedded inside of updatee. For memory safety reasons, Rust doesn't allow that.
The simplest solution would be to pass to update a reference to the field in Updatee that needs to be updated rather than the entire Updatee:
impl Updater {
fn update(&mut self, updatee_value: &mut u64) {
self.value = self.value + 1;
*updatee_value = self.value;
}
}
If multiple fields need to be updated, Updatee could be transformed into a wrapper around the Updater and another struct that holds the fields with the actual data, which could be safely passed to update.

Related Links

remove duplicates from vector of custom struct
error with % operator inside closure
How can I open a file with the standard text editor?
How to use multiple variables in routes with Nickel?
sdl2-sys won't compile - could not exec the linker: No such file or directory
Cannot borrow as mutable more than once at a time in one code - but can in another very similar
Forcing a move for an implemented `Copy` type
When do I need to specify explicit lifetimes in Rust?
cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable
Getting sequence of bytes (u8) from a char
How does the lifetime work on constant strings?
Conditionally compile only one module at a time
“macro undefined” error in the macro's own doc test
How to get mutable struct from HashMap?
Immutable reference after mutable borrow
How do I compile with “ffast-math”?

Categories

HOME
visual-studio
azure-functions
lambda
keyboard
typelite
qc
couchdb
softlayer
redmine
concurrency
hdfs
ios-simulator
apk
typeerror
websocket
tfs2015
elk-stack
azure-data-lake
angular-meteor
stack-overflow
clickable-image
twitter-oauth
fireloop
lapack
smooks
fullcalendar-scheduler
google-awareness
orange-api
temperature
google-tasks-api
nose
coroutine
renderscript
scheduled-tasks
deepfreeze
amazon-mws
generator
qa
nurbs
math.js
mdns
credit-card
autodesk-model-derivative
python-2.x
xcrun
restful-architecture
ms-dos
strapi
hidden-markov-models
reset
imgur
game-center
ejml
fluid-mac-app-engine
virtual-memory
daz3d
liteide
django-static-precompiler
uiautomatorviewer
network-protocols
lvalue
pecl
front-camera
hmmlearn
gapi
scheduledexecutorservice
nclam
jsf-2.3
carmen
callouts
highlighting
android-async-http
teamviewer
python-cffi
miniconda
calcite
netdatacontractserializer
fantom
bootstrap-wysiwyg
algebraixlib
karma-coverage
abstract-factory
shell-extensions
incron
jcheckbox
sunos
adhoc-polymorphism
encryption-symmetric
flurry-analytics
object-code
communicate
jchartfx
nosql-aggregation
mod-perl
libxml-js
insertion
eager-loading
jbox2d
ofstream
session-0-isolation
thejit
first-class
struts2-s2hibernate
mmc3
firefly-mv
css-friendly
dip
suphp
tessellation
odac
facebook-fbml
rawcontacts
tacit-programming
ning
efs
isapi-redirect

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