Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/serde_derive/src/internals/name.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / serde_derive / src / internals / name.rs` 113 lines ยท 2806 bytes Raw Blame History 1 use crate::internals::attr::{Attr, VecAttr}; 2 use proc_macro2::{Ident, Span, TokenStream}; 3 use quote::ToTokens; 4 use std::cmp::Ordering; 5 use std::collections::BTreeSet; 6 use std::fmt::{self, Display}; 7 use syn::LitStr; 8 9 pub struct MultiName { 10 pub(crate) serialize: Name, 11 pub(crate) serialize_renamed: bool, 12 pub(crate) deserialize: Name, 13 pub(crate) deserialize_renamed: bool, 14 pub(crate) deserialize_aliases: BTreeSet, 15 } 16 17 impl MultiName { 18 pub(crate) fn from_attrs( 19 source_name: Name, 20 ser_name: Attr, 21 de_name: Attr, 22 de_aliases: Option>, 23 ) -> Self { 24 let mut alias_set = BTreeSet::new(); 25 if let Some(de_aliases) = de_aliases { 26 for alias_name in de_aliases.get() { 27 alias_set.insert(alias_name); 28 } 29 } 30 31 let ser_name = ser_name.get(); 32 let ser_renamed = ser_name.is_some(); 33 let de_name = de_name.get(); 34 let de_renamed = de_name.is_some(); 35 MultiName { 36 serialize: ser_name.unwrap_or_else(|| source_name.clone()), 37 serialize_renamed: ser_renamed, 38 deserialize: de_name.unwrap_or(source_name), 39 deserialize_renamed: de_renamed, 40 deserialize_aliases: alias_set, 41 } 42 } 43 44 /// Return the container name for the container when serializing. 45 pub fn serialize_name(&self) -> &Name { 46 &self.serialize 47 } 48 49 /// Return the container name for the container when deserializing. 50 pub fn deserialize_name(&self) -> &Name { 51 &self.deserialize 52 } 53 54 pub(crate) fn deserialize_aliases(&self) -> &BTreeSet { 55 &self.deserialize_aliases 56 } 57 } 58 59 #[derive(Clone)] 60 pub struct Name { 61 pub value: String, 62 pub span: Span, 63 } 64 65 impl ToTokens for Name { 66 fn to_tokens(&self, tokens: &mut TokenStream) { 67 LitStr::new(&self.value, self.span).to_tokens(tokens); 68 } 69 } 70 71 impl Ord for Name { 72 fn cmp(&self, other: &Self) -> Ordering { 73 Ord::cmp(&self.value, &other.value) 74 } 75 } 76 77 impl PartialOrd for Name { 78 fn partial_cmp(&self, other: &Self) -> Option { 79 Some(Ord::cmp(self, other)) 80 } 81 } 82 83 impl Eq for Name {} 84 85 impl PartialEq for Name { 86 fn eq(&self, other: &Self) -> bool { 87 self.value == other.value 88 } 89 } 90 91 impl From<&Ident> for Name { 92 fn from(ident: &Ident) -> Self { 93 Name { 94 value: ident.to_string(), 95 span: ident.span(), 96 } 97 } 98 } 99 100 impl From<&LitStr> for Name { 101 fn from(lit: &LitStr) -> Self { 102 Name { 103 value: lit.value(), 104 span: lit.span(), 105 } 106 } 107 } 108 109 impl Display for Name { 110 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 111 Display::fmt(&self.value, formatter) 112 } 113 }