Closed SpaceManiac closed 9 years ago
Does Lua make any guarantees about how long a reference to a string on the Lua stack is valid for? I think the original design rationale for returning String
was so you couldn't shoot yourself in the foot by keeping an invalid reference around.
The lifetime elision rules tie the lifetime of the returned &str
s to the &mut self
, meaning keeping the &str
around prevents further use of the State
, so the reference won't become invalid.
OK, I admit I'm a bit unfamiliar with those rules. If you can resolve the conflicts I'll go ahead and merge this.
Conflict resolved.
A declaration like pub fn to_str(&mut self, index: Index) -> Option<&str>
desugars to pub fn to_str<'a>(&'a mut self, index: Index) -> Option<&'a str>
, such that the returned reference is considered an extension of the mutable borrow of the State
.
Looks good, thanks!
There are two related sets of changes in this patch:
.to_owned()
to call sites.typename_of
andtypename_at
return'static
strings.I think the first point is pretty important, but the second is somewhat opinion-based. Thoughts?
Also, I should point out I here changed
to_str
to useluaL_tolstring
, which converts more types of things to strings, and doesn't edit the stack in-place. Maybe this isn't desired.