A basic implementation of `ArrayVisitor::visit` using the provided size hint.
(mut access: A)
| 771 | |
| 772 | /// A basic implementation of `ArrayVisitor::visit` using the provided size hint. |
| 773 | pub fn array_visit<'de, A: ArrayAccess<'de>, V: GrowingVec<A::Element>>(mut access: A) -> Result<V, A::Error> { |
| 774 | // Don’t blindly trust length prefixes when reserving initial capacity |
| 775 | // for decoding array elements, as malformed input could generate a huge allocation, |
| 776 | // potentially resulting in an OOM kill. |
| 777 | const RESERVE_ARRAY_ELEMENTS: usize = 4096; |
| 778 | let cap = access.size_hint().unwrap_or(0); |
| 779 | let mut v = V::try_with_capacity(cap.min(RESERVE_ARRAY_ELEMENTS))?; |
| 780 | while let Some(x) = access.next_element()? { |
| 781 | v.push(x) |
| 782 | } |
| 783 | Ok(v) |
| 784 | } |
| 785 | |
| 786 | /// A basic implementation of `ArrayVisitor::validate`. |
| 787 | pub fn array_validate<'de, A: ArrayAccess<'de>>(mut access: A) -> Result<(), A::Error> { |