rust


Is allocating a struct on the heap or having a struct own a heap pointer more idiomatic?


I have a type that occupies too much space to be allocated on the stack:
struct Foo {
lots_of_bytes: [u8; 1024 * 10],
bar: bool,
baz: isize,
}
There are two obvious solutions:
let foo = Box::new(Foo::new());
Or
struct Foo {
lots_of_bytes: Box<[u8; 1024 * 10]>,
bar: bool,
baz: isize,
}
To summarize, I either allocate the entire struct on the heap, or I can have the struct own the heap pointer. Is either of these solutions considered the "idiomatic" solution? Or is it strictly subjective or dependent on context?
I think the question to ask yourself here is: will it ever make sense to place this struct in the stack? If the answer is no, you should probably enforce allocation on the heap. To achieve this, you have two alternatives:
Use lots_of_bytes: Box<[u8; 1024 * 10]>.
Use lots_of_bytes: [u8; 1024 * 10] and ensure that all constructors of Foo return Box<Foo (so it becomes impossible to create a Foo on the stack).
In my opinion, the first alternative is much better:
The struct definition of Foo shows clearly that the data must be stored on the heap.
This only requires boxing lots_of_bytes, instead of the whole struct. This means that bar and baz would be placed on the stack, so you have less indirection.
As for the second alternative, I can't think of any reason to prefer it and have never seen it in the wild.

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
webview
mousemove
haproxy
greasemonkey
static-analysis
hystrix
getopenfilename
fbloginview
live-streaming
heap-dump
word2vec
xsl-fo
significance
nullreferenceexception
psql
yii2-basic-app
maven-surefire-plugin
raima
tracing
strophe.js
webviewclient
points
tsung
dnsmasq
fusion
envoy
locks
reporting
fotorama
rspec-rails
cups
variable-variables
python-jira
vungle-ads
haar-classifier
destroy
flume-twitter
absolute
autorelease
libreoffice-writer
sonicmq
palindrome
popen
portability
mpmovieplayercontroller
rustdoc
finder
cefpython
nclam
xmlbeans
livereload
data-management
pyaudio
nvda
perforce-integrate
deferred-rendering
codecharge
strpos
apple-configurator
android-async-http
jsr363
odesk
deeplink
smart-tv
http.client
nastran
jnlp
domino-designer-eclipse
darwin
flow-js
console.log
subview
html-escape-characters
createfile
sunos
fasterxml
vs-unit-testing-framework
qt5.4
storing-data
refit
flipboard
strcmp
jmenubar
mute
postgres-xc
ax
indesign-server
semicolon
android-authenticator
handshake
jquery-dialog
stretch
popup-blocker
cakeyframeanimation
type-safety
kolite
sigar
differentiation
winsxs
webresponse
anonymous-types
git-log
outlook-2007-addin
reliability
processors
stretchblt
synthesizer

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