Skip to content

Commit e4e179d

Browse files
committed
refactor: Make extra_args a constant
1 parent 6a56ed4 commit e4e179d

File tree

3 files changed

+18
-39
lines changed

3 files changed

+18
-39
lines changed

vm/src/api/function.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,13 @@ vm_function_impl!([dyn Fn] $($args),*);
358358

359359
impl <'vm, $($args,)* R: VmType> FunctionType for fn ($($args),*) -> R {
360360
fn arguments() -> VmIndex {
361-
count!($($args),*) + R::extra_args()
361+
count!($($args),*) + R::EXTRA_ARGS
362362
}
363363
}
364364

365365
impl <'s, $($args,)* R: VmType> FunctionType for dyn Fn($($args),*) -> R + 's {
366366
fn arguments() -> VmIndex {
367-
count!($($args),*) + R::extra_args()
367+
count!($($args),*) + R::EXTRA_ARGS
368368
}
369369
}
370370

@@ -400,10 +400,10 @@ impl<T, $($args,)* R> Function<T, fn($($args),*) -> R>
400400
$(
401401
$args.push(&mut context)?;
402402
)*
403-
for _ in 0..R::extra_args() {
403+
for _ in 0..R::EXTRA_ARGS {
404404
0.push(&mut context).unwrap();
405405
}
406-
let args = count!($($args),*) + R::extra_args();
406+
let args = count!($($args),*) + R::EXTRA_ARGS;
407407
let context = ready!(vm.call_function(cx, context.into_owned(), args))?;
408408
let mut context = context.unwrap();
409409
let result = {
@@ -510,12 +510,12 @@ where
510510
let mut context = vm.current_context();
511511
context.push(self.value.get_variant());
512512

513-
let mut arg_count = R::extra_args();
513+
let mut arg_count = R::EXTRA_ARGS;
514514
for arg in args {
515515
arg_count += 1;
516516
arg.push(&mut context)?;
517517
}
518-
for _ in 0..R::extra_args() {
518+
for _ in 0..R::EXTRA_ARGS {
519519
0.push(&mut context).unwrap();
520520
}
521521
let context = ready!(vm.call_function(cx, context.into_owned(), arg_count))?;
@@ -561,9 +561,7 @@ impl<T: VmType> VmType for TypedBytecode<T> {
561561
T::make_forall_type(vm)
562562
}
563563

564-
fn extra_args() -> VmIndex {
565-
T::extra_args()
566-
}
564+
const EXTRA_ARGS: VmIndex = T::EXTRA_ARGS;
567565
}
568566

569567
impl<'vm, T: VmType> Pushable<'vm> for TypedBytecode<T> {

vm/src/api/mod.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,7 @@ impl<T: VmType> VmType for Unrooted<T> {
321321
T::make_type(vm)
322322
}
323323

324-
fn extra_args() -> VmIndex {
325-
T::extra_args()
326-
}
324+
const EXTRA_ARGS: VmIndex = T::EXTRA_ARGS;
327325
}
328326
impl<'vm, T: VmType> Pushable<'vm> for Unrooted<T> {
329327
fn push(self, context: &mut ActiveThread<'vm>) -> Result<()> {
@@ -460,9 +458,7 @@ pub trait VmType {
460458
/// How many extra arguments a function returning this type requires.
461459
/// Used for abstract types which when used in return position should act like they still need
462460
/// more arguments before they are called
463-
fn extra_args() -> VmIndex {
464-
0
465-
}
461+
const EXTRA_ARGS: VmIndex = 0;
466462
}
467463

468464
/// Trait which allows a possibly asynchronous rust value to be pushed to the virtual machine
@@ -617,9 +613,7 @@ impl<'vm, T: ?Sized + VmType> VmType for PhantomData<T> {
617613
fn make_type(vm: &Thread) -> ArcType {
618614
T::make_type(vm)
619615
}
620-
fn extra_args() -> VmIndex {
621-
T::extra_args()
622-
}
616+
const EXTRA_ARGS: VmIndex = T::EXTRA_ARGS;
623617
}
624618

625619
/// Wrapper which extracts a `Userdata` value from gluon
@@ -640,9 +634,7 @@ where
640634
T::make_forall_type(vm)
641635
}
642636

643-
fn extra_args() -> VmIndex {
644-
T::extra_args()
645-
}
637+
const EXTRA_ARGS: VmIndex = T::EXTRA_ARGS;
646638
}
647639

648640
impl<'vm, 'value, T> Getable<'vm, 'value> for UserdataValue<T>
@@ -783,9 +775,7 @@ where
783775
T::make_type(vm)
784776
}
785777

786-
fn extra_args() -> VmIndex {
787-
T::extra_args()
788-
}
778+
const EXTRA_ARGS: VmIndex = T::EXTRA_ARGS;
789779
}
790780

791781
impl<'vm, T> Pushable<'vm> for WithVM<'vm, T>
@@ -1470,9 +1460,7 @@ where
14701460
fn make_type(vm: &Thread) -> ArcType {
14711461
<F::Output>::make_type(vm)
14721462
}
1473-
fn extra_args() -> VmIndex {
1474-
<F::Output>::extra_args()
1475-
}
1463+
const EXTRA_ARGS: VmIndex = F::Output::EXTRA_ARGS;
14761464
}
14771465

14781466
impl<'vm, F> AsyncPushable<'vm> for FutureResult<F>
@@ -1519,9 +1507,7 @@ impl<T: VmType, E> VmType for RuntimeResult<T, E> {
15191507
T::make_type(vm)
15201508
}
15211509

1522-
fn extra_args() -> VmIndex {
1523-
T::extra_args()
1524-
}
1510+
const EXTRA_ARGS: VmIndex = T::EXTRA_ARGS;
15251511
}
15261512
impl<'vm, T: Pushable<'vm>, E: fmt::Display> Pushable<'vm> for RuntimeResult<T, E> {
15271513
fn push(self, context: &mut ActiveThread<'vm>) -> Result<()> {
@@ -1552,9 +1538,8 @@ where
15521538
let alias = env.find_type_info("std.io.IO").unwrap();
15531539
Type::app(alias.into_type(), collect![T::make_type(vm)])
15541540
}
1555-
fn extra_args() -> VmIndex {
1556-
1
1557-
}
1541+
1542+
const EXTRA_ARGS: VmIndex = 1;
15581543
}
15591544

15601545
impl<'vm, 'value, T: Getable<'vm, 'value>> Getable<'vm, 'value> for IO<T> {
@@ -1750,9 +1735,7 @@ impl<T: VmType> VmType for Pushed<T> {
17501735
T::make_forall_type(vm)
17511736
}
17521737

1753-
fn extra_args() -> VmIndex {
1754-
T::extra_args()
1755-
}
1738+
const EXTRA_ARGS: VmIndex = T::EXTRA_ARGS;
17561739
}
17571740

17581741
impl<'vm, T: VmType> Pushable<'vm> for Pushed<T> {

vm/src/api/opaque.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,7 @@ where
361361
V::make_type(vm)
362362
}
363363

364-
fn extra_args() -> VmIndex {
365-
V::extra_args()
366-
}
364+
const EXTRA_ARGS: VmIndex = V::EXTRA_ARGS;
367365
}
368366

369367
impl<'s, 'value, 'vm, T, V> Pushable<'vm> for Opaque<T, V>

0 commit comments

Comments
 (0)