|
18 | 18 | use crate::values::{
|
19 | 19 | layout::{arena::AValueRepr, avalue::VALUE_STR_A_VALUE_PTR, value::FrozenValue},
|
20 | 20 | string::StarlarkStr,
|
| 21 | + Freezer, Trace, Tracer, Value, |
| 22 | +}; |
| 23 | +use gazebo::{ |
| 24 | + coerce::{Coerce, CoerceKey}, |
| 25 | + prelude::*, |
| 26 | +}; |
| 27 | +use std::{ |
| 28 | + fmt, |
| 29 | + fmt::{Debug, Formatter}, |
| 30 | + intrinsics::copy_nonoverlapping, |
21 | 31 | };
|
22 |
| -use gazebo::prelude::*; |
23 |
| -use std::intrinsics::copy_nonoverlapping; |
24 | 32 |
|
25 | 33 | /// A constant string that can be converted to a [`FrozenValue`].
|
26 | 34 | #[repr(C)] // Must match this layout on the heap
|
@@ -71,13 +79,88 @@ impl<const N: usize> ConstFrozenStringN<N> {
|
71 | 79 | /// assert_eq!(Some("magic"), fv.to_value().unpack_str());
|
72 | 80 | /// ```
|
73 | 81 | #[derive(Copy, Clone, Dupe)]
|
| 82 | +#[repr(C)] |
74 | 83 | pub struct FrozenStringValue(&'static AValueRepr<StarlarkStr>);
|
75 | 84 |
|
| 85 | +impl Debug for FrozenStringValue { |
| 86 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 87 | + f.debug_tuple("FrozenStringValue") |
| 88 | + .field(&self.unpack()) |
| 89 | + .finish() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// Wrapper for a [`Value`] which can only contain a [`StarlarkStr`]. |
| 94 | +#[derive(Copy, Clone, Dupe, Debug)] |
| 95 | +#[repr(C)] |
| 96 | +pub struct StringValue<'v>(Value<'v>); |
| 97 | + |
| 98 | +unsafe impl<'v> Coerce<StringValue<'v>> for FrozenStringValue {} |
| 99 | +unsafe impl<'v> CoerceKey<StringValue<'v>> for FrozenStringValue {} |
| 100 | +unsafe impl<'v> Coerce<StringValue<'v>> for StringValue<'v> {} |
| 101 | +unsafe impl<'v> CoerceKey<StringValue<'v>> for StringValue<'v> {} |
| 102 | + |
76 | 103 | impl FrozenStringValue {
|
77 | 104 | /// Obtain the [`FrozenValue`] for a [`FrozenStringValue`].
|
78 | 105 | pub fn unpack(self) -> FrozenValue {
|
79 | 106 | FrozenValue::new_repr(self.0)
|
80 | 107 | }
|
| 108 | + |
| 109 | + /// Construct without a check that the value contains a string. |
| 110 | + /// |
| 111 | + /// If passed value does not contain a string, it may lead to memory corruption. |
| 112 | + pub(crate) unsafe fn new_unchecked(value: FrozenValue) -> FrozenStringValue { |
| 113 | + debug_assert!(value.unpack_str().is_some()); |
| 114 | + FrozenStringValue(&*(value.0.ptr_value() as *const AValueRepr<StarlarkStr>)) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[allow(dead_code)] // TODO: remove in the following diff |
| 119 | +impl<'v> StringValue<'v> { |
| 120 | + /// Construct without a check that the value contains a string. |
| 121 | + /// |
| 122 | + /// If passed value does not contain a string, it may lead to memory corruption. |
| 123 | + pub(crate) unsafe fn new_unchecked(value: Value<'v>) -> StringValue<'v> { |
| 124 | + debug_assert!(value.unpack_str().is_some()); |
| 125 | + StringValue(value) |
| 126 | + } |
| 127 | + |
| 128 | + /// Construct from a value. Returns [`None`] if a value does not contain a string. |
| 129 | + pub(crate) fn new(value: Value<'v>) -> Option<StringValue<'v>> { |
| 130 | + if value.unpack_str().is_some() { |
| 131 | + Some(StringValue(value)) |
| 132 | + } else { |
| 133 | + None |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + pub(crate) fn unpack_starlark_str(self) -> &'v StarlarkStr { |
| 138 | + debug_assert!(self.0.unpack_str().is_some()); |
| 139 | + unsafe { &self.0.0.unpack_ptr_no_int_unchecked().as_repr().payload } |
| 140 | + } |
| 141 | + |
| 142 | + pub(crate) fn to_value(self) -> Value<'v> { |
| 143 | + self.0 |
| 144 | + } |
| 145 | + |
| 146 | + /// Convert a value to a [`FrozenValue`] using a supplied [`Freezer`]. |
| 147 | + pub(crate) fn freeze(self, freezer: &Freezer) -> anyhow::Result<FrozenStringValue> { |
| 148 | + Ok(unsafe { FrozenStringValue::new_unchecked(freezer.freeze(self.0)?) }) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// Common type for [`StringValue`] and [`FrozenStringValue`]. |
| 153 | +pub(crate) trait StringValueLike<'v>: Debug + Coerce<StringValue<'v>> {} |
| 154 | + |
| 155 | +impl<'v> StringValueLike<'v> for StringValue<'v> {} |
| 156 | + |
| 157 | +impl<'v> StringValueLike<'v> for FrozenStringValue {} |
| 158 | + |
| 159 | +unsafe impl<'v> Trace<'v> for StringValue<'v> { |
| 160 | + fn trace(&mut self, tracer: &Tracer<'v>) { |
| 161 | + self.0.trace(tracer); |
| 162 | + debug_assert!(self.0.unpack_str().is_some()); |
| 163 | + } |
81 | 164 | }
|
82 | 165 |
|
83 | 166 | /// Create a [`FrozenStringValue`].
|
|
0 commit comments