lifetime


Rooting managed value in Rust


I'm trying to convert a str into a Reader and failing hard.
What below code does is extract Reader from str using with_str_reader
use std::io::{with_str_reader,Reader};
pub struct ReadSource<'self> {
priv source: &'self Reader
}
impl<'self> ReadSource<'self> {
fn from_str(data: &'self str) -> ReadSource<'self>{
let r = std::io::with_str_reader(data, |reader| { reader });
ReadSource{
source : r
}
}
}
fn main() {
//Ignore
}
What I get when running is the following
test.rs:12:21: 12:22 error: cannot root managed value long enough
test.rs:12 source : r
^
test.rs:9:54: 14:5 note: managed value would have to be rooted for the lifetime &'self as defined on the block at 9:54...
test.rs:9 fn from_str(data: &'self str) -> ReadSource<'self>{
test.rs:10 let r = std::io::with_str_reader(data, |reader| { reader });
test.rs:11 ReadSource{
test.rs:12 source : r
test.rs:13 }
test.rs:14 }
I know I have to cast something, with self lifetime but I'm unsure what?!
Take a look at https://github.com/mozilla/rust/issues/5723 (and also at the source of std::io::with_str_reader).
Basically, with_str_reader is fundamentally unsound at present, and what you're doing is tripping it up badly, because letting the value escape from the closure like that isn't supposed to be permitted—the with_foo pattern means you can use that inside and inside only.

Related Links

Why can't I store a value and a reference to that value in the same struct?
Rooting managed value in Rust
Nested life times

Categories

HOME
nam
extjs
mod-rewrite
android-studio
mc
mpdf
apple-push-notifications
couchdb
transactions
embedded-resource
ant
openlayers-3
ontology
linker
pyresttest
telegraf
selection
wine
hystrix
getopenfilename
colors
spring-amqp
orange-api
supervisord
nest-api
android-5.1.1-lollipop
user-defined-types
asciimath
jqxgrid
backtracking
introspection
android-service
social-networking
amazon-iam
objectmapper
memory-fences
nsmutablearray
spring-rabbitmq
uivisualeffectview
cheat-engine
gitolite
task-parallel-library
viewcontroller
access-denied
sendinput
autorelease
cloudwatch
aws-kinesis-firehose
image-registration
supertest
phppgadmin
comm
android-sharing
winmerge
dtd
allegro
java-2d
tablespace
data-management
seccomp
coovachilli
android-async-http
gwt-syncproxy
as3-api
voronoi
ajax4jsf
gdl
dynamics-ax-2012-r2
kefir.js
flow-js
clarion
camus
fasterxml
password-recovery
vs-unit-testing-framework
xcode6.3.1
full-text-indexing
coding-efficiency
rails-api
cpu-time
spring-social-linkedin
unrealscript
disjoint-union
web-frameworks
cmath
live-wallpaper
websphere-6.1
taskkill
jzmq
django-apps
server-variables
database-deadlocks
winsxs
fragment-identifier
gaelyk
ccl
ninject-interception
processors
external-sorting
motif
regioninfo
chronic
remote-working
bindable-linq
webclient.uploaddata

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