/// A version of [`Cow`] that can borrow from two different buffers, one of them /// is a deserializer input. /// /// # Lifetimes /// /// - `'i`: lifetime of the data that deserializer borrow from the parsed input /// - `'s`: lifetime of the data that owned by a deserializer pubenum CowRef<'i, 's, B> where
B: ToOwned + ?Sized,
{ /// An input borrowed from the parsed data
Input(&'i B), /// An input borrowed from the buffer owned by another deserializer
Slice(&'s B), /// An input taken from an external deserializer, owned by that deserializer
Owned(<B as ToOwned>::Owned),
} impl<'i, 's, B> Deref for CowRef<'i, 's, B> where
B: ToOwned + ?Sized,
B::Owned: Borrow<B>,
{ type Target = B;
impl<'i, 's> CowRef<'i, 's, str> { /// Supply to the visitor a borrowed string, a string slice, or an owned /// string depending on the kind of input. Unlike [`Self::deserialize_all`], /// only part of [`Self::Owned`] string will be passed to the visitor. /// /// Calls /// - `visitor.visit_borrowed_str` if data borrowed from the input /// - `visitor.visit_str` if data borrowed from another source /// - `visitor.visit_string` if data owned by this type #[cfg(feature = "serialize")] pubfn deserialize_str<V, E>(self, visitor: V) -> Result<V::Value, E> where
V: Visitor<'i>,
E: Error,
{ matchself { Self::Input(s) => visitor.visit_borrowed_str(s), Self::Slice(s) => visitor.visit_str(s), Self::Owned(s) => visitor.visit_string(s),
}
}
/// Calls [`Visitor::visit_bool`] with `true` or `false` if text contains /// [valid] boolean representation, otherwise calls [`Self::deserialize_str`]. /// /// The valid boolean representations are only `"true"`, `"false"`, `"1"`, and `"0"`. /// /// [valid]: https://www.w3.org/TR/xmlschema11-2/#boolean #[cfg(feature = "serialize")] pubfn deserialize_bool<V, E>(self, visitor: V) -> Result<V::Value, E> where
V: Visitor<'i>,
E: Error,
{ matchself.as_ref() { "1" | "true" => visitor.visit_bool(true), "0" | "false" => visitor.visit_bool(false),
_ => self.deserialize_str(visitor),
}
}
}
/// Wrapper around `Vec<u8>` that has a human-readable debug representation: /// printable ASCII symbols output as is, all other output in HEX notation. /// /// Also, when [`serialize`] feature is on, this type deserialized using /// [`deserialize_byte_buf`](serde::Deserializer::deserialize_byte_buf) instead /// of vector's generic [`deserialize_seq`](serde::Deserializer::deserialize_seq) /// /// [`serialize`]: ../index.html#serialize #[derive(PartialEq, Eq)] pubstruct ByteBuf(pub Vec<u8>);
/// Wrapper around `&[u8]` that has a human-readable debug representation: /// printable ASCII symbols output as is, all other output in HEX notation. /// /// Also, when [`serialize`] feature is on, this type deserialized using /// [`deserialize_bytes`](serde::Deserializer::deserialize_bytes) instead /// of vector's generic [`deserialize_seq`](serde::Deserializer::deserialize_seq) /// /// [`serialize`]: ../index.html#serialize #[derive(PartialEq, Eq)] pubstruct Bytes<'de>(pub &'de [u8]);
/// A simple producer of infinite stream of bytes, useful in tests. /// /// Will repeat `chunk` field indefinitely. pubstruct Fountain<'a> { /// That piece of data repeated infinitely... pub chunk: &'a [u8], /// Part of `chunk` that was consumed by BufRead impl pub consumed: usize, /// The overall count of read bytes pub overall_read: u64,
}
impl<'a> io::Read for Fountain<'a> { fn read(&mutself, buf: &mut [u8]) -> io::Result<usize> { let available = &self.chunk[self.consumed..]; let len = buf.len().min(available.len()); let (portion, _) = available.split_at(len);
/// A function to check whether the byte is a whitespace (blank, new line, carriage return or tab). #[inline] pubconstfn is_whitespace(b: u8) -> bool {
matches!(b, b' ' | b'\r' | b'\n' | b'\t')
}
/// Calculates name from an element-like content. Name is the first word in `content`, /// where word boundaries is XML whitespace characters. /// /// 'Whitespace' refers to the definition used by [`is_whitespace`]. #[inline] pubconstfn name_len(mut bytes: &[u8]) -> usize { // Note: A pattern matching based approach (instead of indexing) allows // making the function const. letmut len = 0; whilelet [first, rest @ ..] = bytes { if is_whitespace(*first) { break;
}
len += 1;
bytes = rest;
}
len
}
/// Returns a byte slice with leading XML whitespace bytes removed. /// /// 'Whitespace' refers to the definition used by [`is_whitespace`]. #[inline] pubconstfn trim_xml_start(mut bytes: &[u8]) -> &[u8] { // Note: A pattern matching based approach (instead of indexing) allows // making the function const. whilelet [first, rest @ ..] = bytes { if is_whitespace(*first) {
bytes = rest;
} else { break;
}
}
bytes
}
/// Returns a byte slice with trailing XML whitespace bytes removed. /// /// 'Whitespace' refers to the definition used by [`is_whitespace`]. #[inline] pubconstfn trim_xml_end(mut bytes: &[u8]) -> &[u8] { // Note: A pattern matching based approach (instead of indexing) allows // making the function const. whilelet [rest @ .., last] = bytes { if is_whitespace(*last) {
bytes = rest;
} else { break;
}
}
bytes
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.