Skip to content

Commit

Permalink
lifetimes: Improve lifetimes on OwnedFrozenValueTyped
Browse files Browse the repository at this point in the history
Summary:
For `::new`, the whole point of the function is that you use it to assert that an otherwise shorter-lived reference into the frozen heap will be valid indefinitely, so represent that in the lifetimes.

The others were just... completely wrong

Reviewed By: blackm00n

Differential Revision: D69713843

fbshipit-source-id: acb6956df1068d4bf28089493acae6fbb7af3021
  • Loading branch information
JakobDegen authored and facebook-github-bot committed Feb 18, 2025
1 parent 2ef671c commit 4930190
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions starlark/src/values/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<T: StarlarkValue<'static>> Deref for OwnedFrozenValueTyped<T> {
}
}

impl<T: StarlarkValue<'static>> OwnedFrozenValueTyped<T> {
impl<T: for<'a> StarlarkValue<'a>> OwnedFrozenValueTyped<T> {
/// Create an [`OwnedFrozenValueTyped`] - generally [`OwnedFrozenValueTyped`]s are obtained
/// from downcasting [`OwnedFrozenValue`].
///
Expand All @@ -240,7 +240,11 @@ impl<T: StarlarkValue<'static>> OwnedFrozenValueTyped<T> {
/// let value = heap.alloc("test");
/// unsafe { OwnedFrozenValue::new(heap.into_ref(), value) };
/// ```
pub unsafe fn new(owner: FrozenHeapRef, value: FrozenValueTyped<'static, T>) -> Self {
pub unsafe fn new<'a>(owner: FrozenHeapRef, value: FrozenValueTyped<'a, T>) -> Self {
// SAFETY: The caller has asserted that this heap ref keeps the value alive.
let value = unsafe {
std::mem::transmute::<FrozenValueTyped<'a, T>, FrozenValueTyped<'static, T>>(value)
};
Self { owner, value }
}

Expand Down Expand Up @@ -332,9 +336,9 @@ impl<T: StarlarkValue<'static>> OwnedFrozenValueTyped<T> {
/// Operate on the [`FrozenValue`] stored inside.
/// Safe provided you don't store the argument [`FrozenValue`] after the closure has returned.
/// Using this function is discouraged when possible.
pub fn map<U: StarlarkValue<'static>>(
pub fn map<U: for<'a> StarlarkValue<'a>>(
&self,
f: impl FnOnce(FrozenValueTyped<T>) -> FrozenValueTyped<U>,
f: impl for<'a> FnOnce(FrozenValueTyped<'a, T>) -> FrozenValueTyped<'a, U>,
) -> OwnedFrozenValueTyped<U> {
OwnedFrozenValueTyped {
owner: self.owner.dupe(),
Expand All @@ -343,9 +347,9 @@ impl<T: StarlarkValue<'static>> OwnedFrozenValueTyped<T> {
}

/// Same as [`map`](OwnedFrozenValue::map) above but with [`Result`]
pub fn try_map<U: StarlarkValue<'static>, E>(
pub fn try_map<U: for<'a> StarlarkValue<'a>, E>(
&self,
f: impl FnOnce(FrozenValueTyped<T>) -> Result<FrozenValueTyped<U>, E>,
f: impl for<'a> FnOnce(FrozenValueTyped<'a, T>) -> Result<FrozenValueTyped<'a, U>, E>,
) -> Result<OwnedFrozenValueTyped<U>, E> {
Ok(OwnedFrozenValueTyped {
owner: self.owner.dupe(),
Expand All @@ -354,9 +358,9 @@ impl<T: StarlarkValue<'static>> OwnedFrozenValueTyped<T> {
}

/// Same as [`map`](OwnedFrozenValue::map) above but with [`Option`]
pub fn maybe_map<U: StarlarkValue<'static>>(
pub fn maybe_map<U: for<'a> StarlarkValue<'a>>(
&self,
f: impl FnOnce(FrozenValueTyped<T>) -> Option<FrozenValueTyped<U>>,
f: impl for<'a> FnOnce(FrozenValueTyped<'a, T>) -> Option<FrozenValueTyped<'a, U>>,
) -> Option<OwnedFrozenValueTyped<U>> {
Some(OwnedFrozenValueTyped {
owner: self.owner.dupe(),
Expand Down

0 comments on commit 4930190

Please sign in to comment.