Skip to content

Commit 890d1bb

Browse files
stepanchegfacebook-github-bot
authored andcommitted
UnpackAndDiscard
Summary: Utility for micro-optimization in D59611468. Reviewed By: JakobDegen Differential Revision: D59611470 fbshipit-source-id: bf1e09fd8f626fd0c2ab8ebcb5b30928a6c6fd80
1 parent f556e4f commit 890d1bb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

starlark/src/values.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub use crate::values::types::tuple;
9696
pub use crate::values::unpack::UnpackValue;
9797
pub use crate::values::unpack::UnpackValueError;
9898
pub use crate::values::unpack::UnpackValueErrorInfallible;
99+
pub use crate::values::unpack_and_discard::UnpackAndDiscard;
99100
pub use crate::values::value_of::ValueOf;
100101
pub use crate::values::value_of_unchecked::FrozenValueOfUnchecked;
101102
pub use crate::values::value_of_unchecked::ValueOfUnchecked;
@@ -120,5 +121,6 @@ pub mod type_repr;
120121
pub(crate) mod types;
121122
pub mod typing;
122123
mod unpack;
124+
mod unpack_and_discard;
123125
pub(crate) mod value_of;
124126
pub(crate) mod value_of_unchecked;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2018 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 std::marker::PhantomData;
19+
20+
use allocative::Allocative;
21+
use dupe::Clone_;
22+
use dupe::Copy_;
23+
use dupe::Dupe_;
24+
25+
use crate::values::type_repr::StarlarkTypeRepr;
26+
use crate::values::UnpackValue;
27+
use crate::values::Value;
28+
29+
/// Unpack the value of type `T`, but do not store result.
30+
///
31+
/// This can be used when type needs to be checked, but the unpacked value is not needed.
32+
#[derive(Clone_, Copy_, Dupe_, Allocative)]
33+
#[allocative(bound = "")]
34+
pub struct UnpackAndDiscard<T>(PhantomData<fn() -> T>);
35+
36+
impl<T: StarlarkTypeRepr> StarlarkTypeRepr for UnpackAndDiscard<T> {
37+
type Canonical = T::Canonical;
38+
39+
fn starlark_type_repr() -> crate::typing::Ty {
40+
<Self::Canonical as StarlarkTypeRepr>::starlark_type_repr()
41+
}
42+
}
43+
44+
impl<'v, T: UnpackValue<'v>> UnpackValue<'v> for UnpackAndDiscard<T> {
45+
type Error = T::Error;
46+
47+
fn unpack_value_impl(value: Value<'v>) -> Result<Option<Self>, Self::Error> {
48+
match T::unpack_value_impl(value) {
49+
Ok(None) => Ok(None),
50+
Ok(Some(_)) => Ok(Some(UnpackAndDiscard(PhantomData))),
51+
Err(e) => Err(e),
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)