Q:

Can you express 'valid for lifetime of returned value' in the lifetime system?

This is an issue I've encountered several times.

You have data that needs to go to a task.
You want to safely send the data to the task and process it remotely.
...and you what to wait for the result.

Something like this playpen: http://is.gd/fnhRta
use std::thread::Thread;

#[derive(Debug)]
struct Foo<'a> {
  fp: &'a u32
}

impl<'a> Foo<'a> {
  fn new(v:&'a u32) -> Foo<'a> {
    return Foo {
      fp: v
    };
  }
}

fn main() {
  let value = 100;
  let foo = Foo::new(&value);
  let guard = Thread::scoped(|| {
    println!("{:?}", foo);
  });

  // We know foo is valid in the remote thread, because guard is in the same 
  // scope of foo... but how do we express that using lifetimes?
  guard.join();
}

Is it possible to express this using lifetimes?
Something which would, in essence, only accept a Foo<'a> where <'a> is <= the lifetime of the current { ... } block.

A:

The code as you wrote it now works:
use std::thread;

#[derive(Debug)]
struct Foo<'a> {
    fp: &'a u32
}

impl<'a> Foo<'a> {
    fn new(v: &'a u32) -> Foo<'a> {
        Foo { fp: v }
    }
}

fn main() {
    let value = 100;
    let foo = Foo::new(&value);
    let guard = thread::scoped(|| {
        println!("{:?}", foo);
    });

    // We know foo is valid in the remote thread, because guard is in the same 
    // scope of foo... but how do we express that using lifetimes?
    guard.join();
}

This is likely due to RFC 458, which relaxes the restrictions on Send / Sync. Now, thread::scoped returns a JoinGuard with the same lifetime as the closure.

