c.rs 693 B

1234567891011121314151617181920212223242526
  1. use byteorder::{BigEndian, ByteOrder};
  2. use std::mem::forget;
  3. pub fn forget_rust(buf: Vec<u8>) -> *const u8 {
  4. let ptr = buf.as_ptr();
  5. forget(buf);
  6. ptr
  7. }
  8. #[allow(unused_attributes)]
  9. #[allow(dead_code)]
  10. pub fn reclaim_rust(ptr: *mut u8, length: u32) {
  11. unsafe {
  12. let len: usize = length as usize;
  13. Vec::from_raw_parts(ptr, len, len);
  14. }
  15. }
  16. pub fn extend_front_four_bytes_into_bytes(bytes: &[u8]) -> Vec<u8> {
  17. let mut output = Vec::with_capacity(bytes.len() + 4);
  18. let mut marker_bytes = [0; 4];
  19. BigEndian::write_u32(&mut marker_bytes, bytes.len() as u32);
  20. output.extend_from_slice(&marker_bytes);
  21. output.extend_from_slice(bytes);
  22. output
  23. }