rust


Is only using references the most idiomatic/efficient for “big” structures?


Consider the case of a "big" structure such as one containing several BigInt variables. For example:
struct BigStruct {
x: BigInt,
y: BigInt,
z: BigInt,
}
My current understanding of Rust is the following:
I should not implement Copy on such a structure.
I should manipulate it through references.
Both of these are for performance reasons.
This structure is mainly read-only as its parameters generally don't change once they have been set. Nevertheless, elements of that structure are often used to generate new ones (through operator overriding) as in the following example:
let b1: BigStruct = BigStruct { ... } // Rarely change
let b2: BigStruct = BigStruct { ... } // Rarely change
let b3: BigStruct = &b1 + &b2 // Often combined by operations
Can you confirm that my approach (no Copy, all references) is the most idiomatic/efficient for this use case? It seems to have at least two drawbacks:
If someone want to use my code, they will need to think whether they should use a reference or not from context. I would like to hide this complexity to have the simplest API possible.
I must override each operator twice. For instance, I must provide Add<Self> and Add<&Self> to override addition.
Two (partial) solutions that may solve the initial problem :
Go full references but not as described in the question
Manipulate only &BigStruct ;
Pass &BigStruct as function parameters ;
Override operators only for &BigStruct.
The previous example would be :
let b1: &BigStruct = &BigStruct { ... }
let b2: &BigStruct = &BigStruct { ... }
let b3: &BigStruct = &(b1 + b2)
The main problem of this approach is that functions and operators will return BigStruct rather than &BigStruct which is not convenient for users especially when doing arithmetic (it forces to add a & every time an operation is performed).
Use owned pointers
This use-case may be a legitimate exception for using pointers for efficiency as mentionned in Pointers in Rust : a guide. Concretely, we can :
Manipulate only Box<BigStruct> ;
Pass and return Box<BigStruct> in function ;
Override operators only for Box<BigStruct>.
The previous example would be :
let b1: Box<BigStruct> = Box::new(BigStruct { ... })
let b2: Box<BigStruct> = Box::new(BigStruct { ... })
let b3: Box<BigStruct> = b1 + b2
In this case, arithmetic is very straighforward. This approach has nevertheless some drawbacks :
Working explicitely with pointers is not idiomatic Rust ;
Returning pointers from functions is considered a poor Rust practice.

Related Links

Choose to borrow or pass ownership without duplicating code in Rust
How to get the name of current program without the directory part?
Changing key of HashMap from child method
Cannot build a delete function for a binary search tree in Rust because the borrowed value does not live long enough
Why does AtomicUsize not implement Send?
How can I define a list of functions to call? [duplicate]
How can I tell if a Rust library is deprecated?
error: non-scalar cast: `core::option::Option<i32>` as `usize`
Lifetime of variable in map/flat_map in Rust
Can associated constants be used to initialize fixed size arrays?
Why do Arc and Mutex allow me to change the value of an immutable variable?
Unable to download a package via cargo — timeout
“borrowed value does not live long enough” seems to blame the wrong thing
Is the destructor of Arc guaranteed to be called before upgrading a Weak reference will return None?
Can't find crate for `rayon`
How to write a function that returns Vec<Path>?

Categories

HOME
office365
flask
keyboard
blast
mpdf
lvm
biztalk-2010
ontology
implicit-conversion
ipv6
circleci
cname
jquery-file-upload
pinterest
nose
infrared
gollum-wiki
multi-upload
router
uri
spreadsheetgear
offline-caching
android-service
npoi
django-import-export
jsonresult
autodesk-model-derivative
tightvnc
mapnik
commonsware-cwac
fotorama
universal
om-next
android-collapsingtoolbar
directfb
buildforge
nppexec
bluemixtools
stackpanel
xvfb
cargo
installanywhere
hidden-markov-models
best-buy-api
game-center
distributed-caching
word-cloud
intellij-lombok-plugin
subforms
popen
dblink
http4s
finder
bpms
deviare
system.net.mail
conan
nsdata
declarative
meld
exists
mathnet
fdt
openshift-cartridge
srv-record
canopy
gitlab-omnibus
android-handler
dbmigrate
console.log
camus
nhibernate-criteria
n-tier-architecture
encryption-symmetric
wicket-1.5
cidr
hotswap
cpu-time
code39
armv6
visual-c++-2005
google-code-prettify
rikulo
dibs
asp.net-mvc-3-areas
tridion-2011
getproperty
wiktionary
fragment-identifier
simile
ognl
django-pagination
pyjamas
uninstaller
principles
data-retrieval
index.dat

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