0

Here's a snippet:

struct T {}

fn test<'a>() -> (T, &'a T) {
    let t = T{};
    (t, &t)
}

The compiler tells me the the value t doesn't live long enough, which is not quite true, since the hypothetical caller will own the value, so it's reasonable for him to also get a reference:

let (value, reference) = test(); // The value is alive here, so it's perfectly fine to reference it at this point.

Is there any way to achieve such a behavior? If not, are there any possible workarounds, besides getting a reference on a separate line like so?

let value = test();
let reference = &value;

My actual case is not about a reference, but rather about a marker value that I want not to outlive the returned T, but the principle is the same. And I want the user to only be able to get a single marker, so a method on T is not quite what I want here. Note how my values are immutable: I understand that a reference will cause aliasing and this question is NOT about that.

monnoroch
  • 390
  • 1
  • 10
  • @Shepmaster Thanks! However, I explicitly stated that the question is not about references, but lifetimes. The reference is just an example, I could have put `PhantomData<&T>` there instead, which I now realize I should have done to avoid confusion. – monnoroch Jun 16 '18 at 22:40
  • That doesn't matter. The point of `PhantomData` is to "act like" you have the type even though you don't. You cant move a struct with a self-reference and you can't move a struct that looks like it has a self-reference either. – Shepmaster Jun 16 '18 at 22:44
  • I see. Thanks for your answer! If you don't mind me asking, is there any way to get a marker value that can't outlive a host and can't be obtained twice for the host, which would be enforced in compile-time instead of with panicking in the `Host::get_marker(&self)`? – monnoroch Jun 16 '18 at 22:46
  • FYI, in the future, you should be aware of the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and strive to avoid in when asking questions. – Shepmaster Jun 16 '18 at 22:50
  • What purpose does this marker serve? My first high-level answer would be something like a type that wraps the `T` and only allows access in a controlled fashion with a closure. – Shepmaster Jun 16 '18 at 22:52
  • The purpose is a clever API that doesn't allow the user to call some methods while he doesn't have this marker and some methods that consume it. Yeah, I thought about closures, but it's not as pretty then. Having a panic is fine, just not as pretty as returning it with the host value. But still prettier than closures though. – monnoroch Jun 16 '18 at 22:56
  • Your comments here are too vague for me to provide any useful advice. I'd possibly advise asking a brand new question focused on what you want to do, not how you've attempted to create it. Show examples of how it would be used and what it would disallow doing. You could even show your lifetime-based attempt if you felt it contributed. – Shepmaster Jun 16 '18 at 23:00

0 Answers0