|
| 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