Skip to content

Commit 957be9b

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Move AllocValue for bigint into a separate file
Summary: Following diff D38712858 adds more conversions. Reviewed By: bobyangyf Differential Revision: D38712855 fbshipit-source-id: ebc51bf7e6ad2efa5d6614ef368a4e29e2ec4acf
1 parent a38d4dd commit 957be9b

File tree

2 files changed

+75
-51
lines changed

2 files changed

+75
-51
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2019 The Starlark in Rust Authors.
3+
* Copyright (c) Facebook, Inc. and its affiliates.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use crate::values::type_repr::StarlarkTypeRepr;
19+
use crate::values::types::bigint::StarlarkBigInt;
20+
use crate::values::AllocFrozenValue;
21+
use crate::values::AllocValue;
22+
use crate::values::FrozenHeap;
23+
use crate::values::FrozenValue;
24+
use crate::values::Heap;
25+
use crate::values::Value;
26+
27+
impl StarlarkTypeRepr for u64 {
28+
fn starlark_type_repr() -> String {
29+
i32::starlark_type_repr()
30+
}
31+
}
32+
33+
impl<'v> AllocValue<'v> for u64 {
34+
fn alloc_value(self, heap: &'v Heap) -> Value<'v> {
35+
match i32::try_from(self) {
36+
Ok(x) => Value::new_int(x),
37+
Err(_) => StarlarkBigInt::alloc_bigint(self.into(), heap),
38+
}
39+
}
40+
}
41+
42+
impl AllocFrozenValue for u64 {
43+
fn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue {
44+
match i32::try_from(self) {
45+
Ok(x) => FrozenValue::new_int(x),
46+
Err(_) => StarlarkBigInt::alloc_bigint_frozen(self.into(), heap),
47+
}
48+
}
49+
}
50+
51+
impl StarlarkTypeRepr for i64 {
52+
fn starlark_type_repr() -> String {
53+
i32::starlark_type_repr()
54+
}
55+
}
56+
57+
impl<'v> AllocValue<'v> for i64 {
58+
fn alloc_value(self, heap: &'v Heap) -> Value<'v> {
59+
match i32::try_from(self) {
60+
Ok(x) => Value::new_int(x),
61+
Err(_) => StarlarkBigInt::alloc_bigint(self.into(), heap),
62+
}
63+
}
64+
}
65+
66+
impl AllocFrozenValue for i64 {
67+
fn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue {
68+
match i32::try_from(self) {
69+
Ok(x) => FrozenValue::new_int(x),
70+
Err(_) => StarlarkBigInt::alloc_bigint_frozen(self.into(), heap),
71+
}
72+
}
73+
}

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

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
//! Outside of `i32` range int.
1919
20+
mod convert;
21+
2022
use std::cmp::Ordering;
2123
use std::hash::Hash;
2224
use std::ops::Not;
@@ -32,9 +34,6 @@ use serde::Serialize;
3234
use crate::collections::StarlarkHasher;
3335
use crate::values::float::StarlarkFloat;
3436
use crate::values::num::Num;
35-
use crate::values::type_repr::StarlarkTypeRepr;
36-
use crate::values::AllocFrozenValue;
37-
use crate::values::AllocValue;
3837
use crate::values::FrozenHeap;
3938
use crate::values::FrozenValue;
4039
use crate::values::Heap;
@@ -400,54 +399,6 @@ impl<'v> StarlarkValue<'v> for StarlarkBigInt {
400399
}
401400
}
402401

403-
impl StarlarkTypeRepr for u64 {
404-
fn starlark_type_repr() -> String {
405-
i32::starlark_type_repr()
406-
}
407-
}
408-
409-
impl<'v> AllocValue<'v> for u64 {
410-
fn alloc_value(self, heap: &'v Heap) -> Value<'v> {
411-
match i32::try_from(self) {
412-
Ok(x) => Value::new_int(x),
413-
Err(_) => StarlarkBigInt::alloc_bigint(self.into(), heap),
414-
}
415-
}
416-
}
417-
418-
impl AllocFrozenValue for u64 {
419-
fn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue {
420-
match i32::try_from(self) {
421-
Ok(x) => FrozenValue::new_int(x),
422-
Err(_) => StarlarkBigInt::alloc_bigint_frozen(self.into(), heap),
423-
}
424-
}
425-
}
426-
427-
impl StarlarkTypeRepr for i64 {
428-
fn starlark_type_repr() -> String {
429-
i32::starlark_type_repr()
430-
}
431-
}
432-
433-
impl<'v> AllocValue<'v> for i64 {
434-
fn alloc_value(self, heap: &'v Heap) -> Value<'v> {
435-
match i32::try_from(self) {
436-
Ok(x) => Value::new_int(x),
437-
Err(_) => StarlarkBigInt::alloc_bigint(self.into(), heap),
438-
}
439-
}
440-
}
441-
442-
impl AllocFrozenValue for i64 {
443-
fn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue {
444-
match i32::try_from(self) {
445-
Ok(x) => FrozenValue::new_int(x),
446-
Err(_) => StarlarkBigInt::alloc_bigint_frozen(self.into(), heap),
447-
}
448-
}
449-
}
450-
451402
#[cfg(test)]
452403
mod tests {
453404
use std::hash::Hasher;

0 commit comments

Comments
 (0)