rust


How to return Any from a function?


I am working on type system for a database project. One problem is mapping a type id to a reader that given an type id and address, a function can return given any data type from built in u32, String, to defined structs.
I don't have any problem on writers, like such macro
fn set_val (data: &Any, id:i32, mem_ptr: usize) {
match id {
$(
$id => $io::write(*data.downcast_ref::<$t>().unwrap(), mem_ptr),
)*
_ => (),
}
}
But for the reader Any seems not comfortable to be used as return value because the the trait bound "std::any::Any + 'static: std::marker::Sized" is not satisfied. I also tried to return as a reference, but I am stuck at a lifetime
fn get_val (id:i32, mem_ptr: usize) -> Option<& Any> {
match id {
$(
$id => Some(&$io::read(mem_ptr)),
)*
_ => None,
}
}
which complains missing lifetime specifier. If 'static won't work here due to the return value not live long enough, how can I specify the lifetime here?
PS. The read function from $io returns any kinds of types.
Any is a trait, which means that it is unsized and thus cannot be returned by a function as is.
However, you can try boxing it:
fn get_val (id:i32, mem_ptr: usize) -> Option<Box<Any>> {
match id {
$(
$id => Some(Box::new($io::read(mem_ptr))),
)*
_ => None,
}
}
An example playpen.

Related Links

Why does a mutable reference to a dropped object still count as a mutable reference?
How to use 'on_send' method of Nickel response?
How to build an executable that depends on curl for x86_64-unknown-linux-musl
How can I create a struct with a HashMap where the keys the same type as the container
Issues constraining implementation lifetimes on type without lifetime parameter
How to use recursive associated types in functions?
Right way to share a reference between closures
Borrow checker and function arguments in Rust, correct or over zealous?
Most idiomatic way to create a default struct
How to return an Iterator from a Map [duplicate]
Do all primitive types implement the Copy trait?
Comparison of two floats in Rust to arbitrary level of precision
Is there a convenient way to represent x86 instructions in a struct or other language feature?
Why isn't `regex!` a wrapper for `Regex::new` to offer the same regex matching speed?
Lifetime annotation for closure argument
“overflow while adding drop-check rules” while implementing a fingertree

Categories

HOME
hpoo
webview
mod-rewrite
shinyapps
xcode8.1
whmcs
biztalk
odata
portable-class-library
openflow
flurry
angular2-template
hspi
fasm
pm2
fullcalendar-scheduler
fresco
temperature
asciimath
cloudsim
tfsbuild
adobe-illustrator
liferay-6.2
batch-rename
mime-types
backendless
pubxml
cellular-network
running-object-table
math.js
cube
xenapp
dotcms
infopath2010
windows2012
rspec-rails
custom-keyboard
xerces-c
spinner
cheat-engine
gitolite
sspi
extbase
microkernel
oauth2-playground
httphandler
daz3d
cloudwatch
simple-schema
nslayoutconstraint
msxml
zend-server
degrees
instafeedjs
mathjs
fuzzer
jsf-2.3
mathml
wolfram-language
static-methods
meld
mathnet
django-redis
ajax4jsf
smart-tv
html.actionlink
asp.net-web-api-helppages
360-degrees
compositetype
fasterxml
symja
vs-unit-testing-framework
dc
cout
xhprof
libgcc
hotswap
mft
strcmp
stty
codahale-metrics
coalesce
notin
jbox2d
towers-of-hanoi
nosetests
dip
hungarian-notation
mongrel
resharper-5.1
j2mepolish
ppc
firephp
parentid

Resources

Encrypt Message