]>
git.proxmox.com Git - cargo.git/blob - vendor/clap/src/parser/matches/any_value.rs
2 pub(crate) struct AnyValue
{
3 inner
: std
::sync
::Arc
<dyn std
::any
::Any
+ Send
+ Sync
+ '
static>,
4 // While we can extract `TypeId` from `inner`, the debug repr is of a number, so let's track
5 // the type_name in debug builds.
10 pub(crate) fn new
<V
: std
::any
::Any
+ Clone
+ Send
+ Sync
+ '
static>(inner
: V
) -> Self {
11 let id
= AnyValueId
::of
::<V
>();
12 let inner
= std
::sync
::Arc
::new(inner
);
16 pub(crate) fn downcast_ref
<T
: std
::any
::Any
+ Clone
+ Send
+ Sync
+ '
static>(
19 self.inner
.downcast_ref
::<T
>()
22 pub(crate) fn downcast_into
<T
: std
::any
::Any
+ Clone
+ Send
+ Sync
>(self) -> Result
<T
, Self> {
25 std
::sync
::Arc
::downcast
::<T
>(self.inner
).map_err(|inner
| Self { inner, id }
)?
;
26 let value
= std
::sync
::Arc
::try_unwrap(value
).unwrap_or_else(|arc
| (*arc
).clone());
30 pub(crate) fn type_id(&self) -> AnyValueId
{
35 impl std
::fmt
::Debug
for AnyValue
{
36 fn fmt(&self, f
: &mut std
::fmt
::Formatter
<'_
>) -> Result
<(), std
::fmt
::Error
> {
37 f
.debug_struct("AnyValue").field("inner", &self.id
).finish()
41 #[derive(Copy, Clone)]
42 pub struct AnyValueId
{
43 type_id
: std
::any
::TypeId
,
44 #[cfg(debug_assertions)]
45 type_name
: &'
static str,
49 pub(crate) fn of
<A
: ?Sized
+ '
static>() -> Self {
51 type_id
: std
::any
::TypeId
::of
::<A
>(),
52 #[cfg(debug_assertions)]
53 type_name
: std
::any
::type_name
::<A
>(),
58 impl PartialEq
for AnyValueId
{
59 fn eq(&self, other
: &Self) -> bool
{
60 self.type_id
== other
.type_id
64 impl Eq
for AnyValueId {}
66 impl PartialOrd
for AnyValueId
{
67 fn partial_cmp(&self, other
: &Self) -> Option
<std
::cmp
::Ordering
> {
68 self.type_id
.partial_cmp(&other
.type_id
)
72 impl Ord
for AnyValueId
{
73 fn cmp(&self, other
: &Self) -> std
::cmp
::Ordering
{
74 self.type_id
.cmp(&other
.type_id
)
78 impl std
::hash
::Hash
for AnyValueId
{
79 fn hash
<H
: std
::hash
::Hasher
>(&self, state
: &mut H
) {
80 self.type_id
.hash(state
);
84 impl std
::fmt
::Debug
for AnyValueId
{
85 fn fmt(&self, f
: &mut std
::fmt
::Formatter
<'_
>) -> Result
<(), std
::fmt
::Error
> {
86 #[cfg(not(debug_assertions))]
90 #[cfg(debug_assertions)]
92 f
.debug_struct(self.type_name
).finish()
97 impl<'a
, A
: ?Sized
+ '
static> From
<&'a A
> for AnyValueId
{
98 fn from(_
: &'a A
) -> Self {
106 #[cfg(debug_assertions)]
110 assert_eq
!(format
!("{:?}", AnyValue
::new(5)), "AnyValue { inner: i32 }");