miniserde/
ignore.rs

1use crate::de::{Map, Seq, Visitor};
2use crate::error::Result;
3use alloc::boxed::Box;
4use core::ptr;
5
6impl dyn Visitor {
7    pub fn ignore() -> &'static mut dyn Visitor {
8        static mut IGNORE: Ignore = Ignore;
9
10        // Conceptually we have an array of type [Ignore; ∞] in a static, which
11        // is zero sized, and each caller of `fn ignore` gets a unique one of
12        // them, as if by `&mut *ptr::addr_of_mut!(IGNORE[i++])` for some
13        // appropriately synchronized i.
14        unsafe { &mut *ptr::addr_of_mut!(IGNORE) }
15    }
16}
17
18pub(crate) struct Ignore;
19
20impl Visitor for Ignore {
21    fn null(&mut self) -> Result<()> {
22        Ok(())
23    }
24
25    fn boolean(&mut self, _b: bool) -> Result<()> {
26        Ok(())
27    }
28
29    fn string(&mut self, _s: &str) -> Result<()> {
30        Ok(())
31    }
32
33    fn negative(&mut self, _n: i64) -> Result<()> {
34        Ok(())
35    }
36
37    fn nonnegative(&mut self, _n: u64) -> Result<()> {
38        Ok(())
39    }
40
41    fn float(&mut self, _n: f64) -> Result<()> {
42        Ok(())
43    }
44
45    fn seq(&mut self) -> Result<Box<dyn Seq + '_>> {
46        Ok(Box::new(Ignore))
47    }
48
49    fn map(&mut self) -> Result<Box<dyn Map + '_>> {
50        Ok(Box::new(Ignore))
51    }
52}
53
54impl Seq for Ignore {
55    fn element(&mut self) -> Result<&mut dyn Visitor> {
56        Ok(<dyn Visitor>::ignore())
57    }
58
59    fn finish(&mut self) -> Result<()> {
60        Ok(())
61    }
62}
63
64impl Map for Ignore {
65    fn key(&mut self, _k: &str) -> Result<&mut dyn Visitor> {
66        Ok(<dyn Visitor>::ignore())
67    }
68
69    fn finish(&mut self) -> Result<()> {
70        Ok(())
71    }
72}