Function replace_field_with

Source
pub fn replace_field_with(
    encoded_message: &mut Vec<u8>,
    tag_number: u64,
    replace_with: &[u8],
) -> Option<Vec<u8>>
Expand description

Replaces a field with the specified tag number in the encoded message with the given replacement data.

This function modifies the encoded_message in-place and returns the old field value as an Option<Vec<u8>>.

§Arguments

  • encoded_message - A mutable reference to a Vec<u8> containing the encoded message.
  • tag_number - The tag number of the field to replace.
  • replace_with - A byte slice (&[u8]) containing the replacement data.

§Returns

  • Option<Vec<u8>> - If the field is found and replaced successfully, returns Some(old_value), where old_value is the original field value as a Vec<u8>. If the field is not found or an error occurs, returns None.

§Example

use rustwire::replace_field_with;

let mut encoded_message = vec![0x08, 0x01, 0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67];
let tag_number = 2;
let replace_with = b"Hello";

match replace_field_with(&mut encoded_message, tag_number, replace_with) {
    Some(old_value) => println!("Replaced field value: {:?}", old_value),
    None => println!("Field not found or error occurred"),
}

§Notes

  • This function modifies the encoded_message in-place.
  • The function currently creates a copy of the encoded message during the replacement process. It would be more efficient to overwrite the existing data directly.
  • The function supports the following wire types:
    • Varint (wire type 0)
    • 64-bit (wire type 1)
    • Length-delimited (wire type 2)
    • 32-bit (wire type 5)
  • If the wire type is not supported, the function returns None.