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

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?
Function returning a closure not working inside my filter
How do I get a pointer to a memory location when I know the memory address (without using std::ptr)? [duplicate]
How to expose a Rust `Vec<T>` to FFI?
RegOpenKeyExA Keeps Returning Invalid Paramater
Can not return a Chars iterator because it “does not live long enough”

Categories

HOME
google-chrome
android-studio
functional-programming
shinyapps
d3.js
softlayer
hid
hdfs
ontology
kurento
tfs2010
selection
fasm
match
documentum
xbox360
twisted
cocos2d-android
salt-cloud
http-authentication
libgit2
dashdb
sybase-ase
perfino
introspection
high-availability
amazon-cloudtrail
cube
fatfs
mapnik
nsmutablearray
epplus
char-pointer
pdfminer
netstat
formsauthenticationticket
ms-dos
extbase
android-n
google-now
stat
slideshow
distributed-caching
minikube
spooler
httphandler
liteide
ssi
md5-file
android-sharing
htmlspecialchars
scala-collections
dex
prefetch
fitbit
email-injection
rational-performance-test
pjax
libharu
nsrunloop
declarative
eclemma
codecharge
node-glob
jeditable
persistent-object-store
servicestack-bsd
helm
google-maps-api-2
chicagoboss
calcite
360-degrees
pass-by-value
password-recovery
window-resize
forceclose
spring-social-linkedin
assetic
facebook-game-groups
ctp
application-server
disjoint-union
ax
simplecov
onconfigurationchanged
playorm
nsfont
sigar
haiku
ognl
nsconnection
dip
suphp
bass
odac
parentid

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