Skip to content

Commit baec844

Browse files
committed
Add a test for deserializing bytes + warning fix
Added a test for deserializing raw bytes and added a fix for the unused warning on the BytesText::into_inner function since it is used only with the serialize feature.
1 parent 76ec28d commit baec844

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/de/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,45 @@ mod tests {
570570
}
571571
);
572572
}
573+
574+
#[test]
575+
fn deserialize_bytes() {
576+
#[derive(Debug, PartialEq)]
577+
struct Item {
578+
bytes: Vec<u8>,
579+
}
580+
581+
impl<'de> Deserialize<'de> for Item {
582+
fn deserialize<D>(d: D) -> Result<Self, D::Error>
583+
where
584+
D: serde::de::Deserializer<'de>,
585+
{
586+
struct ItemVisitor;
587+
588+
impl<'de> de::Visitor<'de> for ItemVisitor {
589+
type Value = Item;
590+
591+
fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
592+
fmt.write_str("byte data")
593+
}
594+
595+
fn visit_byte_buf<E: de::Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
596+
Ok(Item { bytes: v })
597+
}
598+
}
599+
600+
Ok(d.deserialize_byte_buf(ItemVisitor)?)
601+
}
602+
}
603+
604+
let s = r#"<item>bytes</item>"#;
605+
let item: Item = from_reader(s.as_bytes()).unwrap();
606+
607+
assert_eq!(
608+
item,
609+
Item {
610+
bytes: "bytes".as_bytes().to_vec(),
611+
}
612+
);
613+
}
573614
}

src/events/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ impl<'a> BytesText<'a> {
473473
}
474474

475475
/// Extracts the inner `Cow` from the `BytesText` event container.
476+
#[cfg(feature = "serialize")]
476477
#[inline]
477478
pub(crate) fn into_inner(self) -> Cow<'a, [u8]> {
478479
self.content

0 commit comments

Comments
 (0)