Skip to content

Commit fef39f0

Browse files
stepanchegfacebook-github-bot
authored andcommitted
UnpackValue for more integer types
Summary: Used in the following diff D38712859. Reviewed By: bobyangyf Differential Revision: D38712854 fbshipit-source-id: 5e6269cb9979a6642bf91526d4495f649aaf4fd4
1 parent df0d436 commit fef39f0

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

starlark/src/values/layout/value.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ impl<'v> Value<'v> {
309309
}
310310
}
311311

312+
pub(crate) fn unpack_integer<I>(self) -> Option<I>
313+
where
314+
I: TryFrom<i32>,
315+
I: TryFrom<&'v BigInt>,
316+
{
317+
match self.unpack_num()? {
318+
Num::Float(_) => None,
319+
Num::Int(x) => I::try_from(x).ok(),
320+
Num::BigInt(x) => x.unpack_integer(),
321+
}
322+
}
323+
312324
/// Obtain the underlying `bool` if it is a boolean.
313325
pub fn unpack_bool(self) -> Option<bool> {
314326
let p = self.0.raw().ptr_value();

starlark/src/values/types/bigint/convert.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::values::AllocValue;
2222
use crate::values::FrozenHeap;
2323
use crate::values::FrozenValue;
2424
use crate::values::Heap;
25+
use crate::values::UnpackValue;
2526
use crate::values::Value;
2627

2728
impl StarlarkTypeRepr for u32 {
@@ -143,3 +144,33 @@ impl AllocFrozenValue for isize {
143144
}
144145
}
145146
}
147+
148+
impl<'v> UnpackValue<'v> for u32 {
149+
fn unpack_value(value: Value<'v>) -> Option<u32> {
150+
value.unpack_integer()
151+
}
152+
}
153+
154+
impl<'v> UnpackValue<'v> for u64 {
155+
fn unpack_value(value: Value<'v>) -> Option<u64> {
156+
value.unpack_integer()
157+
}
158+
}
159+
160+
impl<'v> UnpackValue<'v> for i64 {
161+
fn unpack_value(value: Value<'v>) -> Option<i64> {
162+
value.unpack_integer()
163+
}
164+
}
165+
166+
impl<'v> UnpackValue<'v> for usize {
167+
fn unpack_value(value: Value<'v>) -> Option<usize> {
168+
value.unpack_integer()
169+
}
170+
}
171+
172+
impl<'v> UnpackValue<'v> for isize {
173+
fn unpack_value(value: Value<'v>) -> Option<isize> {
174+
value.unpack_integer()
175+
}
176+
}

starlark/src/values/types/bigint/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ impl StarlarkBigInt {
160160
))
161161
}
162162
}
163+
164+
pub(crate) fn unpack_integer<'v, I: TryFrom<&'v BigInt>>(&'v self) -> Option<I> {
165+
I::try_from(&self.value).ok()
166+
}
163167
}
164168

165169
impl PartialEq<i32> for StarlarkBigInt {

0 commit comments

Comments
 (0)