Live video on the AT Protocol

Change encoding from order preserving to just postcard (varint prefixed vec)

+3 -72
+2 -71
rust/iroh-streamplace/src/node/db.rs
··· 492 492 let mut result = Vec::new(); 493 493 if let Some(s) = stream { 494 494 result.push(b's'); 495 - escape_into([s], &mut result); 495 + postcard::to_io(s, &mut result).unwrap(); 496 496 } else { 497 497 result.push(b'g'); 498 498 } ··· 503 503 pub fn decode_stream_and_key(encoded: &[u8]) -> Option<(Option<Vec<u8>>, Vec<u8>)> { 504 504 match encoded.split_first() { 505 505 Some((b's', mut rest)) => { 506 - let stream = unescape_one(&mut rest)?; 506 + let (stream, rest) = postcard::take_from_bytes(&mut rest).ok()?; 507 507 Some((Some(stream), rest.to_vec())) 508 508 } 509 509 Some((b'g', rest)) => Some((None, rest.to_vec())), 510 510 _ => None, 511 511 } 512 - } 513 - 514 - // these values are needed to keep the order preserved 515 - const ESCAPE: u8 = 1; 516 - const SEPARATOR: u8 = 0; 517 - 518 - /// Escape into an existing vec. 519 - fn escape_into<I, C>(components: I, result: &mut Vec<u8>) 520 - where 521 - I: IntoIterator<Item = C>, 522 - C: AsRef<[u8]>, 523 - { 524 - for segment in components.into_iter() { 525 - for &byte in segment.as_ref() { 526 - match byte { 527 - ESCAPE => result.extend([ESCAPE, ESCAPE]), 528 - SEPARATOR => result.extend([ESCAPE, SEPARATOR]), 529 - _ => result.push(byte), 530 - } 531 - } 532 - result.push(SEPARATOR); 533 - } 534 - // you might think that the trailing separator is unnecessary, but it is needed 535 - // to distinguish between the empty path and the path with one empty component 536 - } 537 - 538 - fn unescape_one(path: &mut &[u8]) -> Option<Vec<u8>> { 539 - let mut segment = Vec::new(); 540 - let mut escape = false; 541 - for (i, &byte) in path.iter().enumerate() { 542 - if escape { 543 - segment.push(byte); 544 - escape = false; 545 - } else { 546 - match byte { 547 - ESCAPE => escape = true, 548 - SEPARATOR => { 549 - *path = &path[i + 1..]; 550 - return Some(segment); 551 - } 552 - _ => segment.push(byte), 553 - } 554 - } 555 - } 556 - None 557 - } 558 - 559 - /// A simple version of unescape. 560 - #[allow(dead_code)] 561 - fn unescape(path: &[u8]) -> Vec<Vec<u8>> { 562 - let mut components = Vec::new(); 563 - let mut segment = Vec::new(); 564 - let mut escape = false; 565 - for &byte in path { 566 - if escape { 567 - segment.push(byte); 568 - escape = false; 569 - } else { 570 - match byte { 571 - ESCAPE => escape = true, 572 - SEPARATOR => { 573 - components.push(segment); 574 - segment = Vec::new(); 575 - } 576 - _ => segment.push(byte), 577 - } 578 - } 579 - } 580 - components 581 512 } 582 513 583 514 pub fn format_bytes(bytes: &[u8]) -> String {
+1 -1
rust/iroh-streamplace/src/node/streams.rs
··· 320 320 break; 321 321 }; 322 322 if let Some(shutdown) = self.handle_api(msg).instrument(trace_span!("api")).await { 323 - shutdown.send(()).ok(); 323 + shutdown.send(()).await.ok(); 324 324 break; 325 325 } 326 326 }