Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ pub trait ByteWriter {

/// Writes a number of bytes.
fn write_bytes(&mut self, v: &[u8]);

/// If this `ByteWriter` is a `Vec<u8>`, returns a mutable reference to
/// `self` as `Some(&mut Vec<u8>)`. Returns `None` otherwise.
fn as_mut_vec(&mut self) -> Option<&mut Vec<u8>> {
None
}
}

impl ByteWriter for Vec<u8> {
Expand All @@ -97,6 +103,10 @@ impl ByteWriter for Vec<u8> {
fn write_bytes(&mut self, v: &[u8]) {
self.extend_from_slice(v);
}

fn as_mut_vec(&mut self) -> Option<&mut Vec<u8>> {
Some(self)
}
}

/// String writer used by decoders. In most cases this will be an owned string.
Expand All @@ -114,6 +124,12 @@ pub trait StringWriter {

/// Writes a string.
fn write_str(&mut self, s: &str);

/// If this `StringWriter` is a `String`, returns a mutable reference to
/// `self` as `Some(&mut String)`. Returns `None` otherwise.
fn as_mut_string(&mut self) -> Option<&mut String> {
None
}
}

impl StringWriter for String {
Expand All @@ -128,6 +144,10 @@ impl StringWriter for String {
fn write_str(&mut self, s: &str) {
self.push_str(s);
}

fn as_mut_string(&mut self) -> Option<&mut String> {
Some(self)
}
}

/// Encoder converting a Unicode string into a byte sequence.
Expand Down