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

Create vector of objects implementing a trait in Rust
Using loop variable from “..” loop causes type conversion?
What is RFC 401's coerce_inner useful for?
Rust Borrow checker only complains about borrowing as mutable multiple times when a function that returns a reference with the same lifetime assigned
How can I remove `let _ : () = …`?
Does if-let with a pair short-circuit?
Rust borrow lasts beyond the scope it's in?
How can I include an arbitrary set of Protobuf-built files without knowing their names?
Specialization of method in inherent impl
Getting reference to object behind Rc
Similar implementations of one trait for many structs
Unable to find unsafe trait implementation
Injecting a Diesel connection into an Iron middleware
Rust function does not have static lifetime?
Can Rust optimise away the bit-wise copy during move of an object someday?
Jack audio client name longer than 4 characters breaks client

Categories

HOME
entity-framework
artificial-intelligence
kivy
vue-resource
dji-sdk
facebook-android-sdk
nexus3
phpstorm-2017.1
freertos
cocos2d-android
word2vec
jaspersoft-studio
coroutine
codefluent
infrared
adminlte
bsd
coreldraw
stackexchange.redis
quantitative-finance
plyr
webviewclient
msdeploy
boolean-logic
ibm-connections
dnsmasq
copy-protection
reporting
knights-tour
counting
runtimeexception
xll
text-decorations
linq.js
google-now
utf
data-uri
icepdf
opal-framework
1wire
liteide
xcglogger
sharpdx
winmerge
nslayoutconstraint
kendo-editor
predicates
msxml
mpmovieplayercontroller
yargs
java-2d
fuzzer
ftp-client
fiber
persistent-object-store
webgrind
m4
phpquery
godeps
variable-length-array
nastran
ambiguity
gitlab-omnibus
css-paged-media
domino-designer-eclipse
algebraixlib
angular-amd
kotlin-android-extensions
prism-4
python-curses
operands
adhoc-polymorphism
qt5.4
communicate
commenting
flipboard
jchartfx
autonumber
application-server
mute
disjoint-union
ui-design
oembed
jsplitpane
playorm
getproperty
thejit
series-40
prototypal-inheritance
exponent
filelock
sql-parametrized-query
directshow.net
jquery-effects
motif
parentid

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