MCPcopy Create free account
hub / github.com/ElementsProject/lightning / from_bytes

Method from_bytes

plugins/lsps-plugin/src/core/tlv.rs:65–94  ·  view source on GitHub ↗
(mut bytes: &[u8])

Source from the content-addressed store, hash-verified

63 }
64
65 pub fn from_bytes(mut bytes: &[u8]) -> Result<Self> {
66 let mut recs = Vec::new();
67 let mut last_type: Option<u64> = None;
68
69 while !bytes.is_empty() {
70 let (t, n1) = decode_bigsize(bytes)?;
71 bytes = &bytes[n1..];
72 let (len, n2) = decode_bigsize(bytes)?;
73 bytes = &bytes[n2..];
74
75 let l = usize::try_from(len).map_err(|_| TlvError::Overflow)?;
76 if bytes.len() < l {
77 return Err(TlvError::Truncated.into());
78 }
79 let v = bytes[..l].to_vec();
80 bytes = &bytes[l..];
81
82 if let Some(prev) = last_type {
83 if t == prev {
84 return Err(TlvError::DuplicateType(t).into());
85 }
86 if t < prev {
87 return Err(TlvError::NotSorted.into());
88 }
89 }
90 last_type = Some(t);
91 recs.push(TlvRecord { type_: t, value: v });
92 }
93 Ok(TlvStream(recs))
94 }
95
96 pub fn from_bytes_with_length_prefix(bytes: &[u8]) -> Result<Self> {
97 if bytes.is_empty() {

Callers 2

test_websocketFunction · 0.45
test_wss_proxyFunction · 0.45

Calls 3

decode_bigsizeFunction · 0.85
to_vecMethod · 0.80
TlvStreamClass · 0.70

Tested by 2

test_websocketFunction · 0.36
test_wss_proxyFunction · 0.36