Skip to main content

rustc_structures/
crate_type.rs

1use rustc_serialize::ContextOrd;
2#[cfg(feature = "nightly")]
3use {
4    rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash},
5    rustc_span::{Symbol, sym},
6};
7
8/// Crate type, as specified by `#![crate_type]`
9#[derive(#[automatically_derived]
impl ::core::marker::Copy for CrateType { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for CrateType { }
#[automatically_derived]
impl ::core::clone::Clone for CrateType {
    #[inline]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for CrateType {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_c_like_enum_write_str(f,
            "Executable Dylib Rlib StaticLib Cdylib ProcMacro Sdylib",
            match self {
                CrateType::Executable => 0usize,
                CrateType::Dylib => 11usize,
                CrateType::Rlib => 17usize,
                CrateType::StaticLib => 22usize,
                CrateType::Cdylib => 32usize,
                CrateType::ProcMacro => 39usize,
                CrateType::Sdylib => 49usize,
            })
    }
}Debug, #[automatically_derived]
impl ::core::hash::Hash for CrateType {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
            state)
    }
}Hash, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for CrateType { }
#[automatically_derived]
impl ::core::cmp::PartialEq for CrateType {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
            ::core::intrinsics::discriminant_value(other)
    }
}PartialEq, #[automatically_derived]
impl ::core::default::Default for CrateType {
    #[inline]
    fn default() -> Self { Self::Rlib }
}Default, #[automatically_derived]
impl ::core::cmp::PartialOrd for CrateType {
    #[inline]
    fn partial_cmp(&self, other: &Self)
        -> ::core::option::Option<::core::cmp::Ordering> {
        ::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
    }
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Eq for CrateType { }Eq, #[automatically_derived]
impl ::core::cmp::Ord for CrateType {
    #[inline]
    fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
        ::core::cmp::Ord::cmp(&::core::intrinsics::discriminant_value(self),
            &::core::intrinsics::discriminant_value(other))
    }
}Ord)]
10#[cfg_attr(feature = "nightly", derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for CrateType {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    CrateType::Executable => {}
                    CrateType::Dylib => {}
                    CrateType::Rlib => {}
                    CrateType::StaticLib => {}
                    CrateType::Cdylib => {}
                    CrateType::ProcMacro => {}
                    CrateType::Sdylib => {}
                }
            }
        }
    };StableHash, const _: () =
    {
        impl<__E: ::rustc_serialize::Encoder>
            ::rustc_serialize::Encodable<__E> for CrateType {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        CrateType::Executable => { 0usize }
                        CrateType::Dylib => { 1usize }
                        CrateType::Rlib => { 2usize }
                        CrateType::StaticLib => { 3usize }
                        CrateType::Cdylib => { 4usize }
                        CrateType::ProcMacro => { 5usize }
                        CrateType::Sdylib => { 6usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
            }
        }
    };Encodable_NoContext, const _: () =
    {
        impl<__D: ::rustc_serialize::Decoder>
            ::rustc_serialize::Decodable<__D> for CrateType {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { CrateType::Executable }
                    1usize => { CrateType::Dylib }
                    2usize => { CrateType::Rlib }
                    3usize => { CrateType::StaticLib }
                    4usize => { CrateType::Cdylib }
                    5usize => { CrateType::ProcMacro }
                    6usize => { CrateType::Sdylib }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `CrateType`, expected 0..7, actual {0}",
                                n));
                    }
                }
            }
        }
    };Decodable_NoContext))]
11pub enum CrateType {
12    /// `#![crate_type = "bin"]`
13    Executable,
14    /// `#![crate_type = "dylib"]`
15    Dylib,
16    /// `#![crate_type = "rlib"]` or `#![crate_type = "lib"]`
17    #[default]
18    Rlib,
19    /// `#![crate_type = "staticlib"]`
20    StaticLib,
21    /// `#![crate_type = "cdylib"]`
22    Cdylib,
23    /// `#![crate_type = "proc-macro"]`
24    ProcMacro,
25    /// `#![crate_type = "sdylib"]`
26    // Unstable; feature(export_stable)
27    Sdylib,
28}
29
30impl<Ctx> ContextOrd<Ctx> for CrateType {
31    fn stable_cmp_with_context(_context: Ctx) -> impl FnMut(&Self, &Self) -> std::cmp::Ordering {
32        <Self as Ord>::cmp
33    }
34}
35
36#[cfg(feature = "nightly")]
37impl CrateType {
38    /// Pairs of each `#[crate_type] = "..."` value and the crate type it resolves to
39    pub fn all() -> &'static [(Symbol, Self)] {
40        if true {
    {
        match (&CrateType::default(), &CrateType::Rlib) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(CrateType::default(), CrateType::Rlib);
41        &[
42            (sym::lib, CrateType::Rlib),
43            (sym::rlib, CrateType::Rlib),
44            (sym::dylib, CrateType::Dylib),
45            (sym::cdylib, CrateType::Cdylib),
46            (sym::staticlib, CrateType::StaticLib),
47            (sym::proc_dash_macro, CrateType::ProcMacro),
48            (sym::bin, CrateType::Executable),
49            (sym::sdylib, CrateType::Sdylib),
50        ]
51    }
52
53    /// Same as [`CrateType::all`], but does not include unstable options.
54    /// Used for diagnostics.
55    pub fn all_stable() -> &'static [(Symbol, Self)] {
56        if true {
    {
        match (&CrateType::default(), &CrateType::Rlib) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(CrateType::default(), CrateType::Rlib);
57        &[
58            (sym::lib, CrateType::Rlib),
59            (sym::rlib, CrateType::Rlib),
60            (sym::dylib, CrateType::Dylib),
61            (sym::cdylib, CrateType::Cdylib),
62            (sym::staticlib, CrateType::StaticLib),
63            (sym::proc_dash_macro, CrateType::ProcMacro),
64            (sym::bin, CrateType::Executable),
65        ]
66    }
67}
68
69impl CrateType {
70    pub fn has_metadata(self) -> bool {
71        match self {
72            CrateType::Rlib | CrateType::Dylib | CrateType::ProcMacro => true,
73            CrateType::Executable
74            | CrateType::Cdylib
75            | CrateType::StaticLib
76            | CrateType::Sdylib => false,
77        }
78    }
79}
80
81#[cfg(feature = "nightly")]
82impl TryFrom<Symbol> for CrateType {
83    type Error = ();
84
85    fn try_from(value: Symbol) -> Result<Self, Self::Error> {
86        Ok(match value {
87            sym::bin => CrateType::Executable,
88            sym::dylib => CrateType::Dylib,
89            sym::staticlib => CrateType::StaticLib,
90            sym::cdylib => CrateType::Cdylib,
91            sym::rlib => CrateType::Rlib,
92            sym::lib => CrateType::default(),
93            sym::proc_dash_macro => CrateType::ProcMacro,
94            sym::sdylib => CrateType::Sdylib,
95            _ => return Err(()),
96        })
97    }
98}
99
100impl std::fmt::Display for CrateType {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        match *self {
103            CrateType::Executable => "bin".fmt(f),
104            CrateType::Dylib => "dylib".fmt(f),
105            CrateType::Rlib => "rlib".fmt(f),
106            CrateType::StaticLib => "staticlib".fmt(f),
107            CrateType::Cdylib => "cdylib".fmt(f),
108            CrateType::ProcMacro => "proc-macro".fmt(f),
109            CrateType::Sdylib => "sdylib".fmt(f),
110        }
111    }
112}
113
114#[cfg(feature = "nightly")]
115impl rustc_error_messages::IntoDiagArg for CrateType {
116    fn into_diag_arg(
117        self,
118        _: &mut Option<std::path::PathBuf>,
119    ) -> rustc_error_messages::DiagArgValue {
120        self.to_string().into_diag_arg(&mut None)
121    }
122}