Closed
Description
@graydon and I were talking on IRC about using conditions for serializations error handling today. He convinced me to try out using subtasks to parse and catch failures, but unfortunately doing this means that we can't use serialization error handling on types with managed pointers.
This problem would go away if we could somehow move managed pointers from task A to task B. Even though it's generally unsafe to send a managed pointer across tasks, it's theoretically safe if it's the last thing a task does before it dies.
Here's an obviously wrong, only-works-for-ints example function:
fn try_managed<T>(f: fn~() -> @T) -> Result<@T, ()> {
let result: Result<*libc::c_void, ()> = do task::try {
unsafe {
let v = f();
cast::forget(v);
cast::reinterpret_cast::<@T, *libc::c_void>(&v)
}
};
match move result {
Ok(move v) => {
unsafe {
let v = cast::reinterpret_cast::<*libc::c_void, @T>(&v);
cast::bump_box_refcount(v);
Ok(move v)
}
}
Err(move e) => Err(e),
}
}
fn main() {
let r = do try_managed {
@5
};
match r {
Err(_) => io::println("err"),
Ok(x) => io::println(fmt!("%?", *x)),
}
}
It segfaults for more complicated data structures, like ~str
s.