Skip to content

Commit cf3c35d

Browse files
committed
feat: add pack_to_value
1 parent 5a5ec08 commit cf3c35d

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

msgpacker/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "msgpacker"
3-
version = "0.4.3"
3+
version = "0.4.4"
44
authors = ["Victor Lopez <[email protected]>"]
55
categories = ["compression", "encoding", "parser-implementations"]
66
edition = "2021"

msgpacker/src/lib.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,36 @@ pub use extension::Extension;
3030
#[cfg(feature = "derive")]
3131
pub use msgpacker_derive::MsgPacker;
3232

33+
/// Packs the provided packable value into a vector.
34+
pub fn pack_to_vec<T>(value: &T) -> Vec<u8>
35+
where
36+
T: Packable,
37+
{
38+
let mut bytes = Vec::new();
39+
40+
value.pack(&mut bytes);
41+
42+
bytes
43+
}
44+
3345
/// A packable type.
3446
pub trait Packable {
3547
/// Pack a value into the extendable buffer, returning the amount of written bytes.
3648
fn pack<T>(&self, buf: &mut T) -> usize
3749
where
3850
T: Extend<u8>;
51+
52+
/// Packs the value into a vector of bytes.
53+
fn pack_to_vec<T>(&self) -> Vec<u8> {
54+
let mut bytes = Vec::new();
55+
56+
self.pack(&mut bytes);
57+
58+
bytes
59+
}
3960
}
4061

41-
impl<'a, X> Packable for &'a X
62+
impl<X> Packable for &X
4263
where
4364
X: Packable,
4465
{
@@ -50,7 +71,7 @@ where
5071
}
5172
}
5273

53-
impl<'a, X> Packable for &'a mut X
74+
impl<X> Packable for &mut X
5475
where
5576
X: Packable,
5677
{

0 commit comments

Comments
 (0)