Skip to content

Commit

Permalink
Replace Coerce with RefCast in ListRef and TupleRef
Browse files Browse the repository at this point in the history
Summary: Following diff D56123012 removes `#[derive(Coerce)]` for inner.

Reviewed By: JakobDegen

Differential Revision: D56123010

fbshipit-source-id: fae8bfb56e69dee1cfca3c9340cc6ebb7861cf0b
  • Loading branch information
stepancheg authored and facebook-github-bot committed Apr 15, 2024
1 parent ca997d0 commit 3bd2fb4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions starlark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ num-bigint = "0.4.3"
num-traits = "0.2"
once_cell = "1.8"
paste = "1.0"
ref-cast = "1.0.18"
regex = "1.5.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
19 changes: 9 additions & 10 deletions starlark/src/values/types/list/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,31 @@ use std::fmt;
use std::fmt::Display;
use std::ops::Deref;

use crate as starlark;
use ref_cast::ref_cast_custom;
use ref_cast::RefCastCustom;

use crate::coerce::coerce;
use crate::typing::Ty;
use crate::values::list::value::display_list;
use crate::values::list::value::FrozenListData;
use crate::values::list::value::ListGen;
use crate::values::type_repr::StarlarkTypeRepr;
use crate::values::types::list::value::ListData;
use crate::values::Coerce;
use crate::values::FrozenValue;
use crate::values::UnpackValue;
use crate::values::Value;
use crate::values::ValueLike;

/// Reference to list content (mutable or frozen).
#[repr(transparent)]
#[derive(Coerce)]
#[derive(RefCastCustom)]
pub struct ListRef<'v> {
pub(crate) content: [Value<'v>],
}

/// Reference to frozen list content.
#[repr(transparent)]
#[derive(Coerce)]
#[derive(RefCastCustom)]
pub struct FrozenListRef {
pub(crate) content: [FrozenValue],
}
Expand All @@ -51,9 +52,8 @@ impl<'v> ListRef<'v> {
/// `type([])`, which is `"list"`.
pub const TYPE: &'static str = ListData::TYPE;

pub(crate) fn new<'a>(slice: &'a [Value<'v>]) -> &'a ListRef<'v> {
coerce(slice)
}
#[ref_cast_custom]
pub(crate) fn new<'a>(slice: &'a [Value<'v>]) -> &'a ListRef<'v>;

/// List elements.
pub fn content(&self) -> &[Value<'v>] {
Expand Down Expand Up @@ -90,9 +90,8 @@ impl FrozenListRef {
/// `type([])`, which is `"list"`.
pub const TYPE: &'static str = ListRef::TYPE;

fn new(slice: &[FrozenValue]) -> &FrozenListRef {
coerce(slice)
}
#[ref_cast_custom]
fn new(slice: &[FrozenValue]) -> &FrozenListRef;

/// Downcast to the frozen list.
///
Expand Down
20 changes: 13 additions & 7 deletions starlark/src/values/types/tuple/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

use crate as starlark;
use crate::coerce::coerce;
use crate::coerce::Coerce;
use ref_cast::ref_cast_custom;
use ref_cast::RefCastCustom;

use crate::typing::Ty;
use crate::values::type_repr::StarlarkTypeRepr;
use crate::values::types::tuple::value::FrozenTuple;
Expand All @@ -28,15 +28,15 @@ use crate::values::Value;
use crate::values::ValueLike;

/// Reference to tuple data in Starlark heap.
#[derive(Coerce, Debug)]
#[derive(RefCastCustom, Debug)]
#[repr(transparent)]
pub struct TupleRef<'v> {
contents: [Value<'v>],
}

/// Reference to tuple data in frozen Starlark heap.
#[repr(transparent)]
#[derive(Coerce, Debug)]
#[derive(RefCastCustom, Debug)]
pub struct FrozenTupleRef {
contents: [FrozenValue],
}
Expand All @@ -45,9 +45,12 @@ impl<'v> TupleRef<'v> {
/// `type(())`, which is `"tuple"`.
pub const TYPE: &'static str = FrozenTupleRef::TYPE;

#[ref_cast_custom]
fn new(slice: &'v [Value<'v>]) -> &'v TupleRef<'v>;

/// Downcast a value to a tuple.
pub fn from_value(value: Value<'v>) -> Option<&'v TupleRef<'v>> {
Some(coerce(Tuple::from_value(value)?.content()))
Some(Self::new(Tuple::from_value(value)?.content()))
}

/// Downcast a value to a tuple.
Expand Down Expand Up @@ -75,9 +78,12 @@ impl FrozenTupleRef {
/// `type(())`, which is `"tuple"`.
pub const TYPE: &'static str = FrozenTuple::TYPE;

#[ref_cast_custom]
fn new(slice: &'static [FrozenValue]) -> &'static FrozenTupleRef;

/// Downcast a value to a tuple.
pub fn from_frozen_value(value: FrozenValue) -> Option<&'static FrozenTupleRef> {
Some(coerce(value.downcast_ref::<FrozenTuple>()?.content()))
Some(Self::new(value.downcast_ref::<FrozenTuple>()?.content()))
}

/// Number of elements.
Expand Down

0 comments on commit 3bd2fb4

Please sign in to comment.