Skip to content

Commit

Permalink
Move OwnedFrozenRef into own module
Browse files Browse the repository at this point in the history
Summary: To add more code there.

Reviewed By: JakobDegen

Differential Revision: D61058558

fbshipit-source-id: 4fa7f3519a3c49b61dce8771d0997de7f5299666
  • Loading branch information
stepancheg authored and facebook-github-bot committed Aug 17, 2024
1 parent f3b2e36 commit 2d34199
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 93 deletions.
3 changes: 2 additions & 1 deletion starlark-rust/starlark/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//! so may serve as interesting inspiration for writing your own values, in addition to occurring in Starlark programs.
pub use layout::alloc_static_simple::AllocStaticSimple;
pub use owned_frozen_ref::OwnedFrozenRef;
pub use starlark_derive::starlark_attrs;
pub use starlark_derive::starlark_value;
pub use starlark_derive::AllocFrozenValue;
Expand All @@ -50,7 +51,6 @@ pub use crate::values::demand::Demand;
pub use crate::values::error::ValueError;
pub use crate::values::freeze::Freeze;
pub use crate::values::frozen_ref::FrozenRef;
pub use crate::values::frozen_ref::OwnedFrozenRef;
pub use crate::values::iter::StarlarkIterator;
pub use crate::values::layout::complex::ValueTypedComplex;
pub use crate::values::layout::heap::heap_type::Freezer;
Expand Down Expand Up @@ -114,6 +114,7 @@ pub(crate) mod iter;
pub(crate) mod layout;
pub(crate) mod num;
mod owned;
pub(crate) mod owned_frozen_ref;
pub(crate) mod recursive_repr_or_json_guard;
mod stack_guard;
pub(crate) mod starlark_type_id;
Expand Down
91 changes: 0 additions & 91 deletions starlark-rust/starlark/src/values/frozen_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ use std::sync::atomic;
use allocative::Allocative;
use dupe::Clone_;
use dupe::Copy_;
use dupe::Dupe;
use dupe::Dupe_;

use crate::values::Freeze;
use crate::values::Freezer;
use crate::values::FrozenHeapRef;
use crate::values::Trace;
use crate::values::Tracer;

Expand Down Expand Up @@ -208,92 +206,3 @@ impl<T> AtomicFrozenRefOption<T> {
);
}
}

/// Same as a `FrozenRef`, but it keeps itself alive by storing a reference to the owning heap.
///
/// Usually constructed from an `OwnedFrozenValueTyped`.
#[derive(Clone, Dupe, Allocative)]
pub struct OwnedFrozenRef<T: ?Sized + 'static> {
owner: FrozenHeapRef,
// Invariant: this FrozenValue must be kept alive by the `owner` field.
value: FrozenRef<'static, T>,
}

impl<T: ?Sized> OwnedFrozenRef<T> {
/// Creates a new `OwnedFrozenRef` pointing at the given value.
///
/// ## Safety
///
/// The reference must be kept alive by the owning heap
pub unsafe fn new_unchecked(value: &'static T, owner: FrozenHeapRef) -> OwnedFrozenRef<T> {
OwnedFrozenRef {
owner,
value: FrozenRef::new(value),
}
}

/// Returns a reference to the underlying value.
pub fn as_ref<'a>(&'a self) -> &'a T {
self.value.as_ref()
}

/// Converts `self` into a new reference that points at something reachable from the previous.
///
/// See the caveats on `[starlark::values::OwnedFrozenValue::map]`
pub fn map<F, U: ?Sized>(self, f: F) -> OwnedFrozenRef<U>
where
for<'v> F: FnOnce(&'v T) -> &'v U,
{
OwnedFrozenRef {
owner: self.owner,
value: self.value.map(f),
}
}

/// Fallible map the reference to another one.
pub fn try_map_result<F, U: ?Sized, E>(self, f: F) -> Result<OwnedFrozenRef<U>, E>
where
for<'v> F: FnOnce(&'v T) -> Result<&'v U, E>,
{
Ok(OwnedFrozenRef {
owner: self.owner,
value: self.value.try_map_result(f)?,
})
}

/// Optionally map the reference to another one.
pub fn try_map_option<F, U: ?Sized>(self, f: F) -> Option<OwnedFrozenRef<U>>
where
for<'v> F: FnOnce(&'v T) -> Option<&'v U>,
{
Some(OwnedFrozenRef {
owner: self.owner,
value: self.value.try_map_option(f)?,
})
}

/// Get a reference to the owning frozen heap
pub fn owner(&self) -> &FrozenHeapRef {
&self.owner
}
}

impl<T: ?Sized + fmt::Debug> fmt::Debug for OwnedFrozenRef<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.value, f)
}
}

impl<T: ?Sized + fmt::Display> fmt::Display for OwnedFrozenRef<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.value, f)
}
}

impl<T: ?Sized> Deref for OwnedFrozenRef<T> {
type Target = T;

fn deref(&self) -> &T {
self.as_ref()
}
}
2 changes: 1 addition & 1 deletion starlark-rust/starlark/src/values/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ use dupe::Dupe_;
use crate::cast::transmute;
use crate::typing::Ty;
use crate::values::none::NoneType;
use crate::values::owned_frozen_ref::OwnedFrozenRef;
use crate::values::type_repr::StarlarkTypeRepr;
use crate::values::AllocFrozenValue;
use crate::values::FrozenHeap;
use crate::values::FrozenHeapRef;
use crate::values::FrozenValue;
use crate::values::FrozenValueTyped;
use crate::values::OwnedFrozenRef;
use crate::values::StarlarkValue;
use crate::values::Value;

Expand Down
115 changes: 115 additions & 0 deletions starlark-rust/starlark/src/values/owned_frozen_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2019 The Starlark in Rust Authors.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use std::fmt;
use std::fmt::Formatter;
use std::ops::Deref;

use allocative::Allocative;
use dupe::Dupe;

use crate::values::FrozenHeapRef;
use crate::values::FrozenRef;

/// Same as a `FrozenRef`, but it keeps itself alive by storing a reference to the owning heap.
///
/// Usually constructed from an `OwnedFrozenValueTyped`.
#[derive(Clone, Dupe, Allocative)]
pub struct OwnedFrozenRef<T: ?Sized + 'static> {
owner: FrozenHeapRef,
// Invariant: this FrozenValue must be kept alive by the `owner` field.
value: FrozenRef<'static, T>,
}

impl<T: ?Sized> OwnedFrozenRef<T> {
/// Creates a new `OwnedFrozenRef` pointing at the given value.
///
/// ## Safety
///
/// The reference must be kept alive by the owning heap
pub unsafe fn new_unchecked(value: &'static T, owner: FrozenHeapRef) -> OwnedFrozenRef<T> {
OwnedFrozenRef {
owner,
value: FrozenRef::new(value),
}
}

/// Returns a reference to the underlying value.
pub fn as_ref<'a>(&'a self) -> &'a T {
self.value.as_ref()
}

/// Converts `self` into a new reference that points at something reachable from the previous.
///
/// See the caveats on `[starlark::values::OwnedFrozenValue::map]`
pub fn map<F, U: ?Sized>(self, f: F) -> OwnedFrozenRef<U>
where
for<'v> F: FnOnce(&'v T) -> &'v U,
{
OwnedFrozenRef {
owner: self.owner,
value: self.value.map(f),
}
}

/// Fallible map the reference to another one.
pub fn try_map_result<F, U: ?Sized, E>(self, f: F) -> Result<OwnedFrozenRef<U>, E>
where
for<'v> F: FnOnce(&'v T) -> Result<&'v U, E>,
{
Ok(OwnedFrozenRef {
owner: self.owner,
value: self.value.try_map_result(f)?,
})
}

/// Optionally map the reference to another one.
pub fn try_map_option<F, U: ?Sized>(self, f: F) -> Option<OwnedFrozenRef<U>>
where
for<'v> F: FnOnce(&'v T) -> Option<&'v U>,
{
Some(OwnedFrozenRef {
owner: self.owner,
value: self.value.try_map_option(f)?,
})
}

/// Get a reference to the owning frozen heap
pub fn owner(&self) -> &FrozenHeapRef {
&self.owner
}
}

impl<T: ?Sized + fmt::Debug> fmt::Debug for OwnedFrozenRef<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.value, f)
}
}

impl<T: ?Sized + fmt::Display> fmt::Display for OwnedFrozenRef<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.value, f)
}
}

impl<T: ?Sized> Deref for OwnedFrozenRef<T> {
type Target = T;

fn deref(&self) -> &T {
self.as_ref()
}
}

0 comments on commit 2d34199

Please sign in to comment.