Skip to main content

rustc_structures/
crate_type.rs

1#[cfg(feature = "nightly")]
2use {
3    rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash},
4    rustc_span::{Symbol, sym},
5};
6
7/// Crate type, as specified by `#![crate_type]`
8#[derive(#[automatically_derived]
impl ::core::marker::Copy for CrateType { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CrateType {
    #[inline]
    fn clone(&self) -> CrateType { *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::write_str(f,
            match self {
                CrateType::Executable => "Executable",
                CrateType::Dylib => "Dylib",
                CrateType::Rlib => "Rlib",
                CrateType::StaticLib => "StaticLib",
                CrateType::Cdylib => "Cdylib",
                CrateType::ProcMacro => "ProcMacro",
                CrateType::Sdylib => "Sdylib",
            })
    }
}Debug, #[automatically_derived]
impl ::core::hash::Hash for CrateType {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for CrateType {
    #[inline]
    fn eq(&self, other: &CrateType) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::default::Default for CrateType {
    #[inline]
    fn default() -> CrateType { Self::Rlib }
}Default, #[automatically_derived]
impl ::core::cmp::PartialOrd for CrateType {
    #[inline]
    fn partial_cmp(&self, other: &CrateType)
        -> ::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 {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::Ord for CrateType {
    #[inline]
    fn cmp(&self, other: &CrateType) -> ::core::cmp::Ordering {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr)
    }
}Ord)]
9#[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))]
10pub enum CrateType {
11    /// `#![crate_type = "bin"]`
12    Executable,
13    /// `#![crate_type = "dylib"]`
14    Dylib,
15    /// `#![crate_type = "rlib"]` or `#![crate_type = "lib"]`
16    #[default]
17    Rlib,
18    /// `#![crate_type = "staticlib"]`
19    StaticLib,
20    /// `#![crate_type = "cdylib"]`
21    Cdylib,
22    /// `#![crate_type = "proc-macro"]`
23    ProcMacro,
24    /// `#![crate_type = "sdylib"]`
25    // Unstable; feature(export_stable)
26    Sdylib,
27}
28#[cfg(feature = "nightly")]
29impl CrateType {
30    /// Pairs of each `#[crate_type] = "..."` value and the crate type it resolves to
31    pub fn all() -> &'static [(Symbol, Self)] {
32        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);
33        &[
34            (sym::lib, CrateType::Rlib),
35            (sym::rlib, CrateType::Rlib),
36            (sym::dylib, CrateType::Dylib),
37            (sym::cdylib, CrateType::Cdylib),
38            (sym::staticlib, CrateType::StaticLib),
39            (sym::proc_dash_macro, CrateType::ProcMacro),
40            (sym::bin, CrateType::Executable),
41            (sym::sdylib, CrateType::Sdylib),
42        ]
43    }
44
45    /// Same as [`CrateType::all`], but does not include unstable options.
46    /// Used for diagnostics.
47    pub fn all_stable() -> &'static [(Symbol, Self)] {
48        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);
49        &[
50            (sym::lib, CrateType::Rlib),
51            (sym::rlib, CrateType::Rlib),
52            (sym::dylib, CrateType::Dylib),
53            (sym::cdylib, CrateType::Cdylib),
54            (sym::staticlib, CrateType::StaticLib),
55            (sym::proc_dash_macro, CrateType::ProcMacro),
56            (sym::bin, CrateType::Executable),
57        ]
58    }
59}
60
61impl CrateType {
62    pub fn has_metadata(self) -> bool {
63        match self {
64            CrateType::Rlib | CrateType::Dylib | CrateType::ProcMacro => true,
65            CrateType::Executable
66            | CrateType::Cdylib
67            | CrateType::StaticLib
68            | CrateType::Sdylib => false,
69        }
70    }
71}
72
73#[cfg(feature = "nightly")]
74impl TryFrom<Symbol> for CrateType {
75    type Error = ();
76
77    fn try_from(value: Symbol) -> Result<Self, Self::Error> {
78        Ok(match value {
79            sym::bin => CrateType::Executable,
80            sym::dylib => CrateType::Dylib,
81            sym::staticlib => CrateType::StaticLib,
82            sym::cdylib => CrateType::Cdylib,
83            sym::rlib => CrateType::Rlib,
84            sym::lib => CrateType::default(),
85            sym::proc_dash_macro => CrateType::ProcMacro,
86            sym::sdylib => CrateType::Sdylib,
87            _ => return Err(()),
88        })
89    }
90}
91
92impl std::fmt::Display for CrateType {
93    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        match *self {
95            CrateType::Executable => "bin".fmt(f),
96            CrateType::Dylib => "dylib".fmt(f),
97            CrateType::Rlib => "rlib".fmt(f),
98            CrateType::StaticLib => "staticlib".fmt(f),
99            CrateType::Cdylib => "cdylib".fmt(f),
100            CrateType::ProcMacro => "proc-macro".fmt(f),
101            CrateType::Sdylib => "sdylib".fmt(f),
102        }
103    }
104}
105
106#[cfg(feature = "nightly")]
107impl rustc_error_messages::IntoDiagArg for CrateType {
108    fn into_diag_arg(
109        self,
110        _: &mut Option<std::path::PathBuf>,
111    ) -> rustc_error_messages::DiagArgValue {
112        self.to_string().into_diag_arg(&mut None)
113    }
114}