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/src/private/ser.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 / src / private / ser.rs` 1382 lines ยท 41496 bytes Raw Blame History 1 use crate::lib::*; 2 3 use crate::ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}; 4 5 #[cfg(any(feature = "std", feature = "alloc"))] 6 use self::content::{ 7 Content, ContentSerializer, SerializeStructVariantAsMapValue, SerializeTupleVariantAsMapValue, 8 }; 9 10 /// Used to check that serde(getter) attributes return the expected type. 11 /// Not public API. 12 pub fn constrain(t: &T) -> &T { 13 t 14 } 15 16 /// Not public API. 17 pub fn serialize_tagged_newtype( 18 serializer: S, 19 type_ident: &'static str, 20 variant_ident: &'static str, 21 tag: &'static str, 22 variant_name: &'static str, 23 value: &T, 24 ) -> Result 25 where 26 S: Serializer, 27 T: Serialize, 28 { 29 value.serialize(TaggedSerializer { 30 type_ident, 31 variant_ident, 32 tag, 33 variant_name, 34 delegate: serializer, 35 }) 36 } 37 38 struct TaggedSerializer { 39 type_ident: &'static str, 40 variant_ident: &'static str, 41 tag: &'static str, 42 variant_name: &'static str, 43 delegate: S, 44 } 45 46 enum Unsupported { 47 Boolean, 48 Integer, 49 Float, 50 Char, 51 String, 52 ByteArray, 53 Optional, 54 Sequence, 55 Tuple, 56 TupleStruct, 57 #[cfg(not(any(feature = "std", feature = "alloc")))] 58 Enum, 59 } 60 61 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 62 impl Display for Unsupported { 63 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 64 match *self { 65 Unsupported::Boolean => formatter.write_str("a boolean"), 66 Unsupported::Integer => formatter.write_str("an integer"), 67 Unsupported::Float => formatter.write_str("a float"), 68 Unsupported::Char => formatter.write_str("a char"), 69 Unsupported::String => formatter.write_str("a string"), 70 Unsupported::ByteArray => formatter.write_str("a byte array"), 71 Unsupported::Optional => formatter.write_str("an optional"), 72 Unsupported::Sequence => formatter.write_str("a sequence"), 73 Unsupported::Tuple => formatter.write_str("a tuple"), 74 Unsupported::TupleStruct => formatter.write_str("a tuple struct"), 75 #[cfg(not(any(feature = "std", feature = "alloc")))] 76 Unsupported::Enum => formatter.write_str("an enum"), 77 } 78 } 79 } 80 81 impl TaggedSerializer 82 where 83 S: Serializer, 84 { 85 fn bad_type(self, what: Unsupported) -> S::Error { 86 ser::Error::custom(format_args!( 87 "cannot serialize tagged newtype variant {}::{} containing {}", 88 self.type_ident, self.variant_ident, what 89 )) 90 } 91 } 92 93 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 94 impl Serializer for TaggedSerializer 95 where 96 S: Serializer, 97 { 98 type Ok = S::Ok; 99 type Error = S::Error; 100 101 type SerializeSeq = Impossible; 102 type SerializeTuple = Impossible; 103 type SerializeTupleStruct = Impossible; 104 type SerializeMap = S::SerializeMap; 105 type SerializeStruct = S::SerializeStruct; 106 107 #[cfg(not(any(feature = "std", feature = "alloc")))] 108 type SerializeTupleVariant = Impossible; 109 #[cfg(any(feature = "std", feature = "alloc"))] 110 type SerializeTupleVariant = SerializeTupleVariantAsMapValue; 111 112 #[cfg(not(any(feature = "std", feature = "alloc")))] 113 type SerializeStructVariant = Impossible; 114 #[cfg(any(feature = "std", feature = "alloc"))] 115 type SerializeStructVariant = SerializeStructVariantAsMapValue; 116 117 fn serialize_bool(self, _: bool) -> Result { 118 Err(self.bad_type(Unsupported::Boolean)) 119 } 120 121 fn serialize_i8(self, _: i8) -> Result { 122 Err(self.bad_type(Unsupported::Integer)) 123 } 124 125 fn serialize_i16(self, _: i16) -> Result { 126 Err(self.bad_type(Unsupported::Integer)) 127 } 128 129 fn serialize_i32(self, _: i32) -> Result { 130 Err(self.bad_type(Unsupported::Integer)) 131 } 132 133 fn serialize_i64(self, _: i64) -> Result { 134 Err(self.bad_type(Unsupported::Integer)) 135 } 136 137 fn serialize_u8(self, _: u8) -> Result { 138 Err(self.bad_type(Unsupported::Integer)) 139 } 140 141 fn serialize_u16(self, _: u16) -> Result { 142 Err(self.bad_type(Unsupported::Integer)) 143 } 144 145 fn serialize_u32(self, _: u32) -> Result { 146 Err(self.bad_type(Unsupported::Integer)) 147 } 148 149 fn serialize_u64(self, _: u64) -> Result { 150 Err(self.bad_type(Unsupported::Integer)) 151 } 152 153 fn serialize_f32(self, _: f32) -> Result { 154 Err(self.bad_type(Unsupported::Float)) 155 } 156 157 fn serialize_f64(self, _: f64) -> Result { 158 Err(self.bad_type(Unsupported::Float)) 159 } 160 161 fn serialize_char(self, _: char) -> Result { 162 Err(self.bad_type(Unsupported::Char)) 163 } 164 165 fn serialize_str(self, _: &str) -> Result { 166 Err(self.bad_type(Unsupported::String)) 167 } 168 169 fn serialize_bytes(self, _: &[u8]) -> Result { 170 Err(self.bad_type(Unsupported::ByteArray)) 171 } 172 173 fn serialize_none(self) -> Result { 174 Err(self.bad_type(Unsupported::Optional)) 175 } 176 177 fn serialize_some(self, _: &T) -> Result 178 where 179 T: ?Sized + Serialize, 180 { 181 Err(self.bad_type(Unsupported::Optional)) 182 } 183 184 fn serialize_unit(self) -> Result { 185 let mut map = tri!(self.delegate.serialize_map(Some(1))); 186 tri!(map.serialize_entry(self.tag, self.variant_name)); 187 map.end() 188 } 189 190 fn serialize_unit_struct(self, _: &'static str) -> Result { 191 let mut map = tri!(self.delegate.serialize_map(Some(1))); 192 tri!(map.serialize_entry(self.tag, self.variant_name)); 193 map.end() 194 } 195 196 fn serialize_unit_variant( 197 self, 198 _: &'static str, 199 _: u32, 200 inner_variant: &'static str, 201 ) -> Result { 202 let mut map = tri!(self.delegate.serialize_map(Some(2))); 203 tri!(map.serialize_entry(self.tag, self.variant_name)); 204 tri!(map.serialize_entry(inner_variant, &())); 205 map.end() 206 } 207 208 fn serialize_newtype_struct( 209 self, 210 _: &'static str, 211 value: &T, 212 ) -> Result 213 where 214 T: ?Sized + Serialize, 215 { 216 value.serialize(self) 217 } 218 219 fn serialize_newtype_variant( 220 self, 221 _: &'static str, 222 _: u32, 223 inner_variant: &'static str, 224 inner_value: &T, 225 ) -> Result 226 where 227 T: ?Sized + Serialize, 228 { 229 let mut map = tri!(self.delegate.serialize_map(Some(2))); 230 tri!(map.serialize_entry(self.tag, self.variant_name)); 231 tri!(map.serialize_entry(inner_variant, inner_value)); 232 map.end() 233 } 234 235 fn serialize_seq(self, _: Option) -> Result { 236 Err(self.bad_type(Unsupported::Sequence)) 237 } 238 239 fn serialize_tuple(self, _: usize) -> Result { 240 Err(self.bad_type(Unsupported::Tuple)) 241 } 242 243 fn serialize_tuple_struct( 244 self, 245 _: &'static str, 246 _: usize, 247 ) -> Result { 248 Err(self.bad_type(Unsupported::TupleStruct)) 249 } 250 251 #[cfg(not(any(feature = "std", feature = "alloc")))] 252 fn serialize_tuple_variant( 253 self, 254 _: &'static str, 255 _: u32, 256 _: &'static str, 257 _: usize, 258 ) -> Result { 259 // Lack of push-based serialization means we need to buffer the content 260 // of the tuple variant, so it requires std. 261 Err(self.bad_type(Unsupported::Enum)) 262 } 263 264 #[cfg(any(feature = "std", feature = "alloc"))] 265 fn serialize_tuple_variant( 266 self, 267 _: &'static str, 268 _: u32, 269 inner_variant: &'static str, 270 len: usize, 271 ) -> Result { 272 let mut map = tri!(self.delegate.serialize_map(Some(2))); 273 tri!(map.serialize_entry(self.tag, self.variant_name)); 274 tri!(map.serialize_key(inner_variant)); 275 Ok(SerializeTupleVariantAsMapValue::new( 276 map, 277 inner_variant, 278 len, 279 )) 280 } 281 282 fn serialize_map(self, len: Option) -> Result { 283 let mut map = tri!(self.delegate.serialize_map(len.map(|len| len + 1))); 284 tri!(map.serialize_entry(self.tag, self.variant_name)); 285 Ok(map) 286 } 287 288 fn serialize_struct( 289 self, 290 name: &'static str, 291 len: usize, 292 ) -> Result { 293 let mut state = tri!(self.delegate.serialize_struct(name, len + 1)); 294 tri!(state.serialize_field(self.tag, self.variant_name)); 295 Ok(state) 296 } 297 298 #[cfg(not(any(feature = "std", feature = "alloc")))] 299 fn serialize_struct_variant( 300 self, 301 _: &'static str, 302 _: u32, 303 _: &'static str, 304 _: usize, 305 ) -> Result { 306 // Lack of push-based serialization means we need to buffer the content 307 // of the struct variant, so it requires std. 308 Err(self.bad_type(Unsupported::Enum)) 309 } 310 311 #[cfg(any(feature = "std", feature = "alloc"))] 312 fn serialize_struct_variant( 313 self, 314 _: &'static str, 315 _: u32, 316 inner_variant: &'static str, 317 len: usize, 318 ) -> Result { 319 let mut map = tri!(self.delegate.serialize_map(Some(2))); 320 tri!(map.serialize_entry(self.tag, self.variant_name)); 321 tri!(map.serialize_key(inner_variant)); 322 Ok(SerializeStructVariantAsMapValue::new( 323 map, 324 inner_variant, 325 len, 326 )) 327 } 328 329 #[cfg(not(any(feature = "std", feature = "alloc")))] 330 fn collect_str(self, _: &T) -> Result 331 where 332 T: ?Sized + Display, 333 { 334 Err(self.bad_type(Unsupported::String)) 335 } 336 } 337 338 #[cfg(any(feature = "std", feature = "alloc"))] 339 mod content { 340 use crate::lib::*; 341 342 use crate::ser::{self, Serialize, Serializer}; 343 344 pub struct SerializeTupleVariantAsMapValue { 345 map: M, 346 name: &'static str, 347 fields: Vec, 348 } 349 350 impl SerializeTupleVariantAsMapValue { 351 pub fn new(map: M, name: &'static str, len: usize) -> Self { 352 SerializeTupleVariantAsMapValue { 353 map, 354 name, 355 fields: Vec::with_capacity(len), 356 } 357 } 358 } 359 360 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 361 impl ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue 362 where 363 M: ser::SerializeMap, 364 { 365 type Ok = M::Ok; 366 type Error = M::Error; 367 368 fn serialize_field(&mut self, value: &T) -> Result<(), M::Error> 369 where 370 T: ?Sized + Serialize, 371 { 372 let value = tri!(value.serialize(ContentSerializer::::new())); 373 self.fields.push(value); 374 Ok(()) 375 } 376 377 fn end(mut self) -> Result { 378 tri!(self 379 .map 380 .serialize_value(&Content::TupleStruct(self.name, self.fields))); 381 self.map.end() 382 } 383 } 384 385 pub struct SerializeStructVariantAsMapValue { 386 map: M, 387 name: &'static str, 388 fields: Vec<(&'static str, Content)>, 389 } 390 391 impl SerializeStructVariantAsMapValue { 392 pub fn new(map: M, name: &'static str, len: usize) -> Self { 393 SerializeStructVariantAsMapValue { 394 map, 395 name, 396 fields: Vec::with_capacity(len), 397 } 398 } 399 } 400 401 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 402 impl ser::SerializeStructVariant for SerializeStructVariantAsMapValue 403 where 404 M: ser::SerializeMap, 405 { 406 type Ok = M::Ok; 407 type Error = M::Error; 408 409 fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), M::Error> 410 where 411 T: ?Sized + Serialize, 412 { 413 let value = tri!(value.serialize(ContentSerializer::::new())); 414 self.fields.push((key, value)); 415 Ok(()) 416 } 417 418 fn end(mut self) -> Result { 419 tri!(self 420 .map 421 .serialize_value(&Content::Struct(self.name, self.fields))); 422 self.map.end() 423 } 424 } 425 426 pub enum Content { 427 Bool(bool), 428 429 U8(u8), 430 U16(u16), 431 U32(u32), 432 U64(u64), 433 434 I8(i8), 435 I16(i16), 436 I32(i32), 437 I64(i64), 438 439 F32(f32), 440 F64(f64), 441 442 Char(char), 443 String(String), 444 Bytes(Vec), 445 446 None, 447 Some(Box), 448 449 Unit, 450 UnitStruct(&'static str), 451 UnitVariant(&'static str, u32, &'static str), 452 NewtypeStruct(&'static str, Box), 453 NewtypeVariant(&'static str, u32, &'static str, Box), 454 455 Seq(Vec), 456 Tuple(Vec), 457 TupleStruct(&'static str, Vec), 458 TupleVariant(&'static str, u32, &'static str, Vec), 459 Map(Vec<(Content, Content)>), 460 Struct(&'static str, Vec<(&'static str, Content)>), 461 StructVariant( 462 &'static str, 463 u32, 464 &'static str, 465 Vec<(&'static str, Content)>, 466 ), 467 } 468 469 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 470 impl Serialize for Content { 471 fn serialize(&self, serializer: S) -> Result 472 where 473 S: Serializer, 474 { 475 match *self { 476 Content::Bool(b) => serializer.serialize_bool(b), 477 Content::U8(u) => serializer.serialize_u8(u), 478 Content::U16(u) => serializer.serialize_u16(u), 479 Content::U32(u) => serializer.serialize_u32(u), 480 Content::U64(u) => serializer.serialize_u64(u), 481 Content::I8(i) => serializer.serialize_i8(i), 482 Content::I16(i) => serializer.serialize_i16(i), 483 Content::I32(i) => serializer.serialize_i32(i), 484 Content::I64(i) => serializer.serialize_i64(i), 485 Content::F32(f) => serializer.serialize_f32(f), 486 Content::F64(f) => serializer.serialize_f64(f), 487 Content::Char(c) => serializer.serialize_char(c), 488 Content::String(ref s) => serializer.serialize_str(s), 489 Content::Bytes(ref b) => serializer.serialize_bytes(b), 490 Content::None => serializer.serialize_none(), 491 Content::Some(ref c) => serializer.serialize_some(&**c), 492 Content::Unit => serializer.serialize_unit(), 493 Content::UnitStruct(n) => serializer.serialize_unit_struct(n), 494 Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v), 495 Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c), 496 Content::NewtypeVariant(n, i, v, ref c) => { 497 serializer.serialize_newtype_variant(n, i, v, &**c) 498 } 499 Content::Seq(ref elements) => elements.serialize(serializer), 500 Content::Tuple(ref elements) => { 501 use crate::ser::SerializeTuple; 502 let mut tuple = tri!(serializer.serialize_tuple(elements.len())); 503 for e in elements { 504 tri!(tuple.serialize_element(e)); 505 } 506 tuple.end() 507 } 508 Content::TupleStruct(n, ref fields) => { 509 use crate::ser::SerializeTupleStruct; 510 let mut ts = tri!(serializer.serialize_tuple_struct(n, fields.len())); 511 for f in fields { 512 tri!(ts.serialize_field(f)); 513 } 514 ts.end() 515 } 516 Content::TupleVariant(n, i, v, ref fields) => { 517 use crate::ser::SerializeTupleVariant; 518 let mut tv = tri!(serializer.serialize_tuple_variant(n, i, v, fields.len())); 519 for f in fields { 520 tri!(tv.serialize_field(f)); 521 } 522 tv.end() 523 } 524 Content::Map(ref entries) => { 525 use crate::ser::SerializeMap; 526 let mut map = tri!(serializer.serialize_map(Some(entries.len()))); 527 for (k, v) in entries { 528 tri!(map.serialize_entry(k, v)); 529 } 530 map.end() 531 } 532 Content::Struct(n, ref fields) => { 533 use crate::ser::SerializeStruct; 534 let mut s = tri!(serializer.serialize_struct(n, fields.len())); 535 for &(k, ref v) in fields { 536 tri!(s.serialize_field(k, v)); 537 } 538 s.end() 539 } 540 Content::StructVariant(n, i, v, ref fields) => { 541 use crate::ser::SerializeStructVariant; 542 let mut sv = tri!(serializer.serialize_struct_variant(n, i, v, fields.len())); 543 for &(k, ref v) in fields { 544 tri!(sv.serialize_field(k, v)); 545 } 546 sv.end() 547 } 548 } 549 } 550 } 551 552 pub struct ContentSerializer { 553 error: PhantomData, 554 } 555 556 impl ContentSerializer { 557 pub fn new() -> Self { 558 ContentSerializer { error: PhantomData } 559 } 560 } 561 562 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 563 impl Serializer for ContentSerializer 564 where 565 E: ser::Error, 566 { 567 type Ok = Content; 568 type Error = E; 569 570 type SerializeSeq = SerializeSeq; 571 type SerializeTuple = SerializeTuple; 572 type SerializeTupleStruct = SerializeTupleStruct; 573 type SerializeTupleVariant = SerializeTupleVariant; 574 type SerializeMap = SerializeMap; 575 type SerializeStruct = SerializeStruct; 576 type SerializeStructVariant = SerializeStructVariant; 577 578 fn serialize_bool(self, v: bool) -> Result { 579 Ok(Content::Bool(v)) 580 } 581 582 fn serialize_i8(self, v: i8) -> Result { 583 Ok(Content::I8(v)) 584 } 585 586 fn serialize_i16(self, v: i16) -> Result { 587 Ok(Content::I16(v)) 588 } 589 590 fn serialize_i32(self, v: i32) -> Result { 591 Ok(Content::I32(v)) 592 } 593 594 fn serialize_i64(self, v: i64) -> Result { 595 Ok(Content::I64(v)) 596 } 597 598 fn serialize_u8(self, v: u8) -> Result { 599 Ok(Content::U8(v)) 600 } 601 602 fn serialize_u16(self, v: u16) -> Result { 603 Ok(Content::U16(v)) 604 } 605 606 fn serialize_u32(self, v: u32) -> Result { 607 Ok(Content::U32(v)) 608 } 609 610 fn serialize_u64(self, v: u64) -> Result { 611 Ok(Content::U64(v)) 612 } 613 614 fn serialize_f32(self, v: f32) -> Result { 615 Ok(Content::F32(v)) 616 } 617 618 fn serialize_f64(self, v: f64) -> Result { 619 Ok(Content::F64(v)) 620 } 621 622 fn serialize_char(self, v: char) -> Result { 623 Ok(Content::Char(v)) 624 } 625 626 fn serialize_str(self, value: &str) -> Result { 627 Ok(Content::String(value.to_owned())) 628 } 629 630 fn serialize_bytes(self, value: &[u8]) -> Result { 631 Ok(Content::Bytes(value.to_owned())) 632 } 633 634 fn serialize_none(self) -> Result { 635 Ok(Content::None) 636 } 637 638 fn serialize_some(self, value: &T) -> Result 639 where 640 T: ?Sized + Serialize, 641 { 642 Ok(Content::Some(Box::new(tri!(value.serialize(self))))) 643 } 644 645 fn serialize_unit(self) -> Result { 646 Ok(Content::Unit) 647 } 648 649 fn serialize_unit_struct(self, name: &'static str) -> Result { 650 Ok(Content::UnitStruct(name)) 651 } 652 653 fn serialize_unit_variant( 654 self, 655 name: &'static str, 656 variant_index: u32, 657 variant: &'static str, 658 ) -> Result { 659 Ok(Content::UnitVariant(name, variant_index, variant)) 660 } 661 662 fn serialize_newtype_struct(self, name: &'static str, value: &T) -> Result 663 where 664 T: ?Sized + Serialize, 665 { 666 Ok(Content::NewtypeStruct( 667 name, 668 Box::new(tri!(value.serialize(self))), 669 )) 670 } 671 672 fn serialize_newtype_variant( 673 self, 674 name: &'static str, 675 variant_index: u32, 676 variant: &'static str, 677 value: &T, 678 ) -> Result 679 where 680 T: ?Sized + Serialize, 681 { 682 Ok(Content::NewtypeVariant( 683 name, 684 variant_index, 685 variant, 686 Box::new(tri!(value.serialize(self))), 687 )) 688 } 689 690 fn serialize_seq(self, len: Option) -> Result { 691 Ok(SerializeSeq { 692 elements: Vec::with_capacity(len.unwrap_or(0)), 693 error: PhantomData, 694 }) 695 } 696 697 fn serialize_tuple(self, len: usize) -> Result { 698 Ok(SerializeTuple { 699 elements: Vec::with_capacity(len), 700 error: PhantomData, 701 }) 702 } 703 704 fn serialize_tuple_struct( 705 self, 706 name: &'static str, 707 len: usize, 708 ) -> Result { 709 Ok(SerializeTupleStruct { 710 name, 711 fields: Vec::with_capacity(len), 712 error: PhantomData, 713 }) 714 } 715 716 fn serialize_tuple_variant( 717 self, 718 name: &'static str, 719 variant_index: u32, 720 variant: &'static str, 721 len: usize, 722 ) -> Result { 723 Ok(SerializeTupleVariant { 724 name, 725 variant_index, 726 variant, 727 fields: Vec::with_capacity(len), 728 error: PhantomData, 729 }) 730 } 731 732 fn serialize_map(self, len: Option) -> Result { 733 Ok(SerializeMap { 734 entries: Vec::with_capacity(len.unwrap_or(0)), 735 key: None, 736 error: PhantomData, 737 }) 738 } 739 740 fn serialize_struct( 741 self, 742 name: &'static str, 743 len: usize, 744 ) -> Result { 745 Ok(SerializeStruct { 746 name, 747 fields: Vec::with_capacity(len), 748 error: PhantomData, 749 }) 750 } 751 752 fn serialize_struct_variant( 753 self, 754 name: &'static str, 755 variant_index: u32, 756 variant: &'static str, 757 len: usize, 758 ) -> Result { 759 Ok(SerializeStructVariant { 760 name, 761 variant_index, 762 variant, 763 fields: Vec::with_capacity(len), 764 error: PhantomData, 765 }) 766 } 767 } 768 769 pub struct SerializeSeq { 770 elements: Vec, 771 error: PhantomData, 772 } 773 774 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 775 impl ser::SerializeSeq for SerializeSeq 776 where 777 E: ser::Error, 778 { 779 type Ok = Content; 780 type Error = E; 781 782 fn serialize_element(&mut self, value: &T) -> Result<(), E> 783 where 784 T: ?Sized + Serialize, 785 { 786 let value = tri!(value.serialize(ContentSerializer::::new())); 787 self.elements.push(value); 788 Ok(()) 789 } 790 791 fn end(self) -> Result { 792 Ok(Content::Seq(self.elements)) 793 } 794 } 795 796 pub struct SerializeTuple { 797 elements: Vec, 798 error: PhantomData, 799 } 800 801 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 802 impl ser::SerializeTuple for SerializeTuple 803 where 804 E: ser::Error, 805 { 806 type Ok = Content; 807 type Error = E; 808 809 fn serialize_element(&mut self, value: &T) -> Result<(), E> 810 where 811 T: ?Sized + Serialize, 812 { 813 let value = tri!(value.serialize(ContentSerializer::::new())); 814 self.elements.push(value); 815 Ok(()) 816 } 817 818 fn end(self) -> Result { 819 Ok(Content::Tuple(self.elements)) 820 } 821 } 822 823 pub struct SerializeTupleStruct { 824 name: &'static str, 825 fields: Vec, 826 error: PhantomData, 827 } 828 829 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 830 impl ser::SerializeTupleStruct for SerializeTupleStruct 831 where 832 E: ser::Error, 833 { 834 type Ok = Content; 835 type Error = E; 836 837 fn serialize_field(&mut self, value: &T) -> Result<(), E> 838 where 839 T: ?Sized + Serialize, 840 { 841 let value = tri!(value.serialize(ContentSerializer::::new())); 842 self.fields.push(value); 843 Ok(()) 844 } 845 846 fn end(self) -> Result { 847 Ok(Content::TupleStruct(self.name, self.fields)) 848 } 849 } 850 851 pub struct SerializeTupleVariant { 852 name: &'static str, 853 variant_index: u32, 854 variant: &'static str, 855 fields: Vec, 856 error: PhantomData, 857 } 858 859 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 860 impl ser::SerializeTupleVariant for SerializeTupleVariant 861 where 862 E: ser::Error, 863 { 864 type Ok = Content; 865 type Error = E; 866 867 fn serialize_field(&mut self, value: &T) -> Result<(), E> 868 where 869 T: ?Sized + Serialize, 870 { 871 let value = tri!(value.serialize(ContentSerializer::::new())); 872 self.fields.push(value); 873 Ok(()) 874 } 875 876 fn end(self) -> Result { 877 Ok(Content::TupleVariant( 878 self.name, 879 self.variant_index, 880 self.variant, 881 self.fields, 882 )) 883 } 884 } 885 886 pub struct SerializeMap { 887 entries: Vec<(Content, Content)>, 888 key: Option, 889 error: PhantomData, 890 } 891 892 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 893 impl ser::SerializeMap for SerializeMap 894 where 895 E: ser::Error, 896 { 897 type Ok = Content; 898 type Error = E; 899 900 fn serialize_key(&mut self, key: &T) -> Result<(), E> 901 where 902 T: ?Sized + Serialize, 903 { 904 let key = tri!(key.serialize(ContentSerializer::::new())); 905 self.key = Some(key); 906 Ok(()) 907 } 908 909 fn serialize_value(&mut self, value: &T) -> Result<(), E> 910 where 911 T: ?Sized + Serialize, 912 { 913 let key = self 914 .key 915 .take() 916 .expect("serialize_value called before serialize_key"); 917 let value = tri!(value.serialize(ContentSerializer::::new())); 918 self.entries.push((key, value)); 919 Ok(()) 920 } 921 922 fn end(self) -> Result { 923 Ok(Content::Map(self.entries)) 924 } 925 926 fn serialize_entry(&mut self, key: &K, value: &V) -> Result<(), E> 927 where 928 K: ?Sized + Serialize, 929 V: ?Sized + Serialize, 930 { 931 let key = tri!(key.serialize(ContentSerializer::::new())); 932 let value = tri!(value.serialize(ContentSerializer::::new())); 933 self.entries.push((key, value)); 934 Ok(()) 935 } 936 } 937 938 pub struct SerializeStruct { 939 name: &'static str, 940 fields: Vec<(&'static str, Content)>, 941 error: PhantomData, 942 } 943 944 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 945 impl ser::SerializeStruct for SerializeStruct 946 where 947 E: ser::Error, 948 { 949 type Ok = Content; 950 type Error = E; 951 952 fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), E> 953 where 954 T: ?Sized + Serialize, 955 { 956 let value = tri!(value.serialize(ContentSerializer::::new())); 957 self.fields.push((key, value)); 958 Ok(()) 959 } 960 961 fn end(self) -> Result { 962 Ok(Content::Struct(self.name, self.fields)) 963 } 964 } 965 966 pub struct SerializeStructVariant { 967 name: &'static str, 968 variant_index: u32, 969 variant: &'static str, 970 fields: Vec<(&'static str, Content)>, 971 error: PhantomData, 972 } 973 974 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 975 impl ser::SerializeStructVariant for SerializeStructVariant 976 where 977 E: ser::Error, 978 { 979 type Ok = Content; 980 type Error = E; 981 982 fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), E> 983 where 984 T: ?Sized + Serialize, 985 { 986 let value = tri!(value.serialize(ContentSerializer::::new())); 987 self.fields.push((key, value)); 988 Ok(()) 989 } 990 991 fn end(self) -> Result { 992 Ok(Content::StructVariant( 993 self.name, 994 self.variant_index, 995 self.variant, 996 self.fields, 997 )) 998 } 999 } 1000 } 1001 1002 #[cfg(any(feature = "std", feature = "alloc"))] 1003 pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M); 1004 1005 #[cfg(any(feature = "std", feature = "alloc"))] 1006 impl<'a, M> FlatMapSerializer<'a, M> 1007 where 1008 M: SerializeMap + 'a, 1009 { 1010 fn bad_type(what: Unsupported) -> M::Error { 1011 ser::Error::custom(format_args!( 1012 "can only flatten structs and maps (got {})", 1013 what 1014 )) 1015 } 1016 } 1017 1018 #[cfg(any(feature = "std", feature = "alloc"))] 1019 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 1020 impl<'a, M> Serializer for FlatMapSerializer<'a, M> 1021 where 1022 M: SerializeMap + 'a, 1023 { 1024 type Ok = (); 1025 type Error = M::Error; 1026 1027 type SerializeSeq = Impossible; 1028 type SerializeTuple = Impossible; 1029 type SerializeTupleStruct = Impossible; 1030 type SerializeMap = FlatMapSerializeMap<'a, M>; 1031 type SerializeStruct = FlatMapSerializeStruct<'a, M>; 1032 type SerializeTupleVariant = FlatMapSerializeTupleVariantAsMapValue<'a, M>; 1033 type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>; 1034 1035 fn serialize_bool(self, _: bool) -> Result { 1036 Err(Self::bad_type(Unsupported::Boolean)) 1037 } 1038 1039 fn serialize_i8(self, _: i8) -> Result { 1040 Err(Self::bad_type(Unsupported::Integer)) 1041 } 1042 1043 fn serialize_i16(self, _: i16) -> Result { 1044 Err(Self::bad_type(Unsupported::Integer)) 1045 } 1046 1047 fn serialize_i32(self, _: i32) -> Result { 1048 Err(Self::bad_type(Unsupported::Integer)) 1049 } 1050 1051 fn serialize_i64(self, _: i64) -> Result { 1052 Err(Self::bad_type(Unsupported::Integer)) 1053 } 1054 1055 fn serialize_u8(self, _: u8) -> Result { 1056 Err(Self::bad_type(Unsupported::Integer)) 1057 } 1058 1059 fn serialize_u16(self, _: u16) -> Result { 1060 Err(Self::bad_type(Unsupported::Integer)) 1061 } 1062 1063 fn serialize_u32(self, _: u32) -> Result { 1064 Err(Self::bad_type(Unsupported::Integer)) 1065 } 1066 1067 fn serialize_u64(self, _: u64) -> Result { 1068 Err(Self::bad_type(Unsupported::Integer)) 1069 } 1070 1071 fn serialize_f32(self, _: f32) -> Result { 1072 Err(Self::bad_type(Unsupported::Float)) 1073 } 1074 1075 fn serialize_f64(self, _: f64) -> Result { 1076 Err(Self::bad_type(Unsupported::Float)) 1077 } 1078 1079 fn serialize_char(self, _: char) -> Result { 1080 Err(Self::bad_type(Unsupported::Char)) 1081 } 1082 1083 fn serialize_str(self, _: &str) -> Result { 1084 Err(Self::bad_type(Unsupported::String)) 1085 } 1086 1087 fn serialize_bytes(self, _: &[u8]) -> Result { 1088 Err(Self::bad_type(Unsupported::ByteArray)) 1089 } 1090 1091 fn serialize_none(self) -> Result { 1092 Ok(()) 1093 } 1094 1095 fn serialize_some(self, value: &T) -> Result 1096 where 1097 T: ?Sized + Serialize, 1098 { 1099 value.serialize(self) 1100 } 1101 1102 fn serialize_unit(self) -> Result { 1103 Ok(()) 1104 } 1105 1106 fn serialize_unit_struct(self, _: &'static str) -> Result { 1107 Ok(()) 1108 } 1109 1110 fn serialize_unit_variant( 1111 self, 1112 _: &'static str, 1113 _: u32, 1114 variant: &'static str, 1115 ) -> Result { 1116 self.0.serialize_entry(variant, &()) 1117 } 1118 1119 fn serialize_newtype_struct( 1120 self, 1121 _: &'static str, 1122 value: &T, 1123 ) -> Result 1124 where 1125 T: ?Sized + Serialize, 1126 { 1127 value.serialize(self) 1128 } 1129 1130 fn serialize_newtype_variant( 1131 self, 1132 _: &'static str, 1133 _: u32, 1134 variant: &'static str, 1135 value: &T, 1136 ) -> Result 1137 where 1138 T: ?Sized + Serialize, 1139 { 1140 self.0.serialize_entry(variant, value) 1141 } 1142 1143 fn serialize_seq(self, _: Option) -> Result { 1144 Err(Self::bad_type(Unsupported::Sequence)) 1145 } 1146 1147 fn serialize_tuple(self, _: usize) -> Result { 1148 Err(Self::bad_type(Unsupported::Tuple)) 1149 } 1150 1151 fn serialize_tuple_struct( 1152 self, 1153 _: &'static str, 1154 _: usize, 1155 ) -> Result { 1156 Err(Self::bad_type(Unsupported::TupleStruct)) 1157 } 1158 1159 fn serialize_tuple_variant( 1160 self, 1161 _: &'static str, 1162 _: u32, 1163 variant: &'static str, 1164 _: usize, 1165 ) -> Result { 1166 tri!(self.0.serialize_key(variant)); 1167 Ok(FlatMapSerializeTupleVariantAsMapValue::new(self.0)) 1168 } 1169 1170 fn serialize_map(self, _: Option) -> Result { 1171 Ok(FlatMapSerializeMap(self.0)) 1172 } 1173 1174 fn serialize_struct( 1175 self, 1176 _: &'static str, 1177 _: usize, 1178 ) -> Result { 1179 Ok(FlatMapSerializeStruct(self.0)) 1180 } 1181 1182 fn serialize_struct_variant( 1183 self, 1184 _: &'static str, 1185 _: u32, 1186 inner_variant: &'static str, 1187 _: usize, 1188 ) -> Result { 1189 tri!(self.0.serialize_key(inner_variant)); 1190 Ok(FlatMapSerializeStructVariantAsMapValue::new( 1191 self.0, 1192 inner_variant, 1193 )) 1194 } 1195 } 1196 1197 #[cfg(any(feature = "std", feature = "alloc"))] 1198 pub struct FlatMapSerializeMap<'a, M: 'a>(&'a mut M); 1199 1200 #[cfg(any(feature = "std", feature = "alloc"))] 1201 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 1202 impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M> 1203 where 1204 M: SerializeMap + 'a, 1205 { 1206 type Ok = (); 1207 type Error = M::Error; 1208 1209 fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> 1210 where 1211 T: ?Sized + Serialize, 1212 { 1213 self.0.serialize_key(key) 1214 } 1215 1216 fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> 1217 where 1218 T: ?Sized + Serialize, 1219 { 1220 self.0.serialize_value(value) 1221 } 1222 1223 fn serialize_entry(&mut self, key: &K, value: &V) -> Result<(), Self::Error> 1224 where 1225 K: ?Sized + Serialize, 1226 V: ?Sized + Serialize, 1227 { 1228 self.0.serialize_entry(key, value) 1229 } 1230 1231 fn end(self) -> Result<(), Self::Error> { 1232 Ok(()) 1233 } 1234 } 1235 1236 #[cfg(any(feature = "std", feature = "alloc"))] 1237 pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M); 1238 1239 #[cfg(any(feature = "std", feature = "alloc"))] 1240 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 1241 impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M> 1242 where 1243 M: SerializeMap + 'a, 1244 { 1245 type Ok = (); 1246 type Error = M::Error; 1247 1248 fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> 1249 where 1250 T: ?Sized + Serialize, 1251 { 1252 self.0.serialize_entry(key, value) 1253 } 1254 1255 fn end(self) -> Result<(), Self::Error> { 1256 Ok(()) 1257 } 1258 } 1259 1260 //////////////////////////////////////////////////////////////////////////////////////////////////// 1261 1262 #[cfg(any(feature = "std", feature = "alloc"))] 1263 pub struct FlatMapSerializeTupleVariantAsMapValue<'a, M: 'a> { 1264 map: &'a mut M, 1265 fields: Vec, 1266 } 1267 1268 #[cfg(any(feature = "std", feature = "alloc"))] 1269 impl<'a, M> FlatMapSerializeTupleVariantAsMapValue<'a, M> 1270 where 1271 M: SerializeMap + 'a, 1272 { 1273 fn new(map: &'a mut M) -> Self { 1274 FlatMapSerializeTupleVariantAsMapValue { 1275 map, 1276 fields: Vec::new(), 1277 } 1278 } 1279 } 1280 1281 #[cfg(any(feature = "std", feature = "alloc"))] 1282 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 1283 impl<'a, M> ser::SerializeTupleVariant for FlatMapSerializeTupleVariantAsMapValue<'a, M> 1284 where 1285 M: SerializeMap + 'a, 1286 { 1287 type Ok = (); 1288 type Error = M::Error; 1289 1290 fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> 1291 where 1292 T: ?Sized + Serialize, 1293 { 1294 let value = tri!(value.serialize(ContentSerializer::::new())); 1295 self.fields.push(value); 1296 Ok(()) 1297 } 1298 1299 fn end(self) -> Result<(), Self::Error> { 1300 tri!(self.map.serialize_value(&Content::Seq(self.fields))); 1301 Ok(()) 1302 } 1303 } 1304 1305 //////////////////////////////////////////////////////////////////////////////////////////////////// 1306 1307 #[cfg(any(feature = "std", feature = "alloc"))] 1308 pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> { 1309 map: &'a mut M, 1310 name: &'static str, 1311 fields: Vec<(&'static str, Content)>, 1312 } 1313 1314 #[cfg(any(feature = "std", feature = "alloc"))] 1315 impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M> 1316 where 1317 M: SerializeMap + 'a, 1318 { 1319 fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> { 1320 FlatMapSerializeStructVariantAsMapValue { 1321 map, 1322 name, 1323 fields: Vec::new(), 1324 } 1325 } 1326 } 1327 1328 #[cfg(any(feature = "std", feature = "alloc"))] 1329 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 1330 impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M> 1331 where 1332 M: SerializeMap + 'a, 1333 { 1334 type Ok = (); 1335 type Error = M::Error; 1336 1337 fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> 1338 where 1339 T: ?Sized + Serialize, 1340 { 1341 let value = tri!(value.serialize(ContentSerializer::::new())); 1342 self.fields.push((key, value)); 1343 Ok(()) 1344 } 1345 1346 fn end(self) -> Result<(), Self::Error> { 1347 tri!(self 1348 .map 1349 .serialize_value(&Content::Struct(self.name, self.fields))); 1350 Ok(()) 1351 } 1352 } 1353 1354 pub struct AdjacentlyTaggedEnumVariant { 1355 pub enum_name: &'static str, 1356 pub variant_index: u32, 1357 pub variant_name: &'static str, 1358 } 1359 1360 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 1361 impl Serialize for AdjacentlyTaggedEnumVariant { 1362 fn serialize(&self, serializer: S) -> Result 1363 where 1364 S: Serializer, 1365 { 1366 serializer.serialize_unit_variant(self.enum_name, self.variant_index, self.variant_name) 1367 } 1368 } 1369 1370 // Error when Serialize for a non_exhaustive remote enum encounters a variant 1371 // that is not recognized. 1372 pub struct CannotSerializeVariant(pub T); 1373 1374 #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] 1375 impl Display for CannotSerializeVariant 1376 where 1377 T: Debug, 1378 { 1379 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 1380 write!(formatter, "enum variant cannot be serialized: {:?}", self.0) 1381 } 1382 }