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

Should I use Rust 1.0-alpha or the nightly version?
How to do a binary search on a Vec of floats?
Having a child modify its parent
How do I access the data sent over UDP?
How to idiomatically copy a slice?
Recursive types and arrays
Can I determine the zero value of generic types?
How to slice a large Vec<i32> as &[u8]?
How to parse i64 from a string? [duplicate]
Why is variable scope dependent on the definition order?
Warning on “unstable” directive
How can I pass a socket as an argument to a function being called within a thread?
error: cannot move out of borrowed content on &mut self
Why does a reference not live long enough in case of “as_slice”?
Getting the error “error: the trait `core::marker::Sized` is not implemented” when trying to return the value from a vector
Traits with associated type in templates

Categories

HOME
webview
events
magento2
libgdx
windows-8.1
opencv-python
d3.js
exchange-server
celery
liferay-7
stored-procedures
bellman-ford
match
lapack
watch-os-3
spring-amqp
jquery-file-upload
trace32
temperature
web-deployment
mixpanel
jqxgrid
pickle
checkout
libgit2
spring-ldap
backtracking
mime-types
web-push
tracing
clipboard
msdeploy
network-analysis
jackrabbit-oak
code-climate
bonita
autodesk-model-derivative
windowsiot
wampsharp
cucumber-junit
sql-execution-plan
advanced-filter
counting
android-canvas
twilio-php
reset
access-denied
optionaldataexception
type-inference
rightnow-crm
dredd
scala-breeze
nusoap
nivo-slider
portability
test-data
http-basic-authentication
front-camera
adobe-reader
maatwebsite-excel
oracle-fusion-apps
allegro
controllers
metabase
livereload
svnserve
wininet
maven-release-plugin
eclemma
exists
mathnet
voronoi
webgrind
largenumber
astropy
supercomputers
artisan
mxe
maven-shade-plugin
kognitio-wx2
vs-unit-testing-framework
iplimage
qt5.4
mfmessagecomposeview
git-reset
xulrunner
windows-taskbar
saga
axacropdf
significant-digits
icenium
red-system
struts2-json-plugin
story
live-wallpaper
clearinterval
getopt-long
exponent
dropshadow
type-safety
mmc3
server-variables
representation
azure-acs
cleartype
yahoo-maps
subtext
tacit-programming
sqlsitemapprovider

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