pub struct Difference<S: ExtendVec + 'static = Vec<u8>> { /* private fields */ }
Expand description

A delta between the local data and the data the Signature represents.

Implementations

Changes the block size to block_size, shrinking the Segment::Unknowns in the process. This results in a smaller diff.

Consider Self::minify_with_builder for using custom signature settings.

Errors

tl;dr, you can Option::unwrap this if it comes straight from Signature::diff or this very function. If it’s untrusted data, handle the errors.

Returns MinifyError::NewLarger if block_size >= Self::block_size and MinifyError::NotMultiple if Self::block_size is not a multiple of block_size. MinifyError::SuccessiveUnknowns is returned if two Segment::Unknown are after each other. Returns MinifyError::Zero if block_size == 0.

Changes the block size to block_size, shrinking the Segment::Unknowns in the process. This results in a smaller diff.

signature_builder takes the block size and gives the signature builder to use.

Panics

Panics if signature_builder returns a SignatureBuilder with a different block_size from what’s supplied.

Errors

tl;dr, you can Option::unwrap this if it comes straight from Signature::diff or this very function. If it’s untrusted data, handle the errors.

Returns MinifyError::NewLarger if block_size >= Self::block_size and MinifyError::NotMultiple if Self::block_size is not a multiple of block_size. MinifyError::SuccessiveUnknowns is returned if two Segment::Unknown are after each other. Returns MinifyError::Zero if block_size == 0.

Create an empty diff that does nothing when applied.

len is the length of the data, both before and after. If this is smaller than the target, it’ll truncate it (in whole block_size segments). If len is longer, applying will return an error.

block_size is simply the size of the blocks. Not that relevant here, but used by other methods on Difference.

Panics

Panics if block_size is 0.

Returns a reference to all the internal Segments.

This can be used for implementing algorithms other than Self::apply to apply the data.

agde uses this to convert from this format to their Section style.

Returns a mutable reference to all the internal Segments.

Don’t use this unless you know what you’re doing.

Using this function, you can change the building blocks of the diff. This can be useful for transforming it to use another ExtendVec for compact metadata storage (which can later be used to revert).

Turns this difference into it’s list of segments.

Prefer to use Self::segments if you don’t plan on consuming the internal data.

Map the ExtendVecs of the Segment::Unknowns with f.

The second argument of f is the start of S the applied data.

Creates a new difference with the same length and values. Consider using Difference::map.

Panics

Panics if NS doesn’t have the same ExtendVec::len as S.

Map the ExtendVecs of the Segment::Unknowns with f.

The second argument of f is the start of S the applied data.

Creates a new difference with the same length and values. Still allocates Difference::segments vector. Consider using Difference::map_ref.

Panics

Panics if NS doesn’t have the same ExtendVec::len as S.

The block size used by this diff.

Approximates the size this takes up in memory or when serializing it with a binary serializer. The returned value is in bytes.

Get the length of the original data - the data fed to SignatureBuilder::write.

Set the length of the original data.

See Self::original_data_len for more details.

Set the block size.

Use Difference::minify to also trim down the size of the contained data.

block_size must be small than Self::block_size. Self::block_size must also be dividable by it.

Errors

Returns MinifyError::NewLarger if block_size >= Self::block_size and MinifyError::NotMultiple if Self::block_size is not a multiple of block_size. Returns MinifyError::Zero if block_size == 0.

Returns whether or not applying this diff is impossible to do on a single Vec.

base_len is the length of base passed to Self::apply or Self::apply_in_place.

If the returned value is true, the apply function will try to read data from parts of the Vec already overridden. You should be able to use a single Vec if the returned value is false.

Examples
let base_data = b"This is a document everyone has. It's about some new difference library.";
let target_data = b"This is a document only I have. It's about some new difference library.";
let mut base_data = base_data.to_vec();

let mut signature = Signature::new(128);
signature.write(&base_data);
let signature = signature.finish();

let diff = signature.diff(target_data);

// This is the small diff you could serialize with Serde and send.
let minified = diff.minify(8, &base_data)
    .expect("This won't panic, as the data hasn't changed from calling the other functions.");

let data = if minified.apply_overlaps(base_data.len()) {
    let mut data = Vec::new();
    minified.apply(&base_data, &mut data);
    data
} else {
    minified.apply_in_place(&mut base_data);
    base_data
};

assert_eq!(data, target_data);

Returns whether or not applying this diff is impossible to do on a single Vec.

base_len is the length of base passed to Self::apply or Self::apply_in_place.

If the returned value is true, the apply function will try to read data from parts of the Vec already overridden. You should be able to use a single Vec if the returned value is false.

The adaptive end part means that if base isn’t the same as fed to SignatureBuilder::write, we’ll try to write any additional written data to the end. This makes it feasible to apply diffs after modifying base, without all modifications being overridden.

Examples

See Self::apply_overlaps.

Apply diff to the base data base, appending the result to out.

Consider checking Self::apply_overlaps and calling Self::apply_in_place to remove the need for the Vec.

Security

The diff should be sanitized if input is suspected to be malicious.

Errors

Returns ApplyError::RefOutOfBounds if a reference is out of bounds of the base. If base is the same data written to SignatureBuilder::write, this error will not occur, granted this diff is derived from that Signature.

Ok to unwrap if Self::in_bounds.

Apply diff to the base data base, appending the result to out.

Consider checking Self::apply_overlaps and calling Self::apply_in_place to remove the need for the Vec.

The adaptive end part means that if base isn’t the same as fed to SignatureBuilder::write, we’ll try to write any additional written data to the end. This makes it feasible to apply diffs after modifying base, without all modifications being overridden.

Security

The diff should be sanitized if input is suspected to be malicious.

Errors

Returns ApplyError::RefOutOfBounds if a reference is out of bounds of the base. If base is the same data written to SignatureBuilder::write, this error will not occur, granted this diff is derived from that Signature.

Ok to unwrap if Self::in_bounds.

Apply diff to the base data base, mutating the base data. You MUST check that this is possible using Self::apply_overlaps. Neglecting that step will result in data loss and erroneous data. This WILL NOT give an error in case of breaking that contract.

Security

The diff should be sanitized if input is suspected to be malicious.

Errors

If this returns an error, consider base to be bogus data.

Returns ApplyError::RefOutOfBounds if a reference is out of bounds of the base.

Ok to unwrap if Self::in_bounds.

Examples

See Self::apply_overlaps.

Apply diff to the base data base, mutating the base data. You MUST check that this is possible using Self::apply_overlaps_adaptive_end. Neglecting that step will result in data loss and erroneous data. This WILL NOT give an error in case of breaking that contract.

The adaptive end part means that if base isn’t the same as fed to SignatureBuilder::write, we’ll try to write any additional written data to the end. This makes it feasible to apply diffs after modifying base, without all modifications being overridden.

Security

The diff should be sanitized if input is suspected to be malicious.

Errors

If this returns an error, consider base to be bogus data.

Returns ApplyError::RefOutOfBounds if a reference is out of bounds of the base.

Ok to unwrap if Self::in_bounds.

Examples

See Self::apply_overlaps.

Reverts current to what it would have been before applying this diff. This completely overrides target.

Say I have the data: hello there - from vim and the diff is [Segment::Ref(0..8)], the new data (current) will be hello th. The length of the original data is stored, but the lost bytes are unrecoverable. They are filled with fill_byte. That should probably be b' ' for text applications.

Security

The diff should be sanitized if input is suspected to be malicious.

Errors

Returns ApplyError::RefOutOfBounds if a reference is out of bounds of the base.

Returns true if this diff is a no-op.

Checks self if it gets base to target, without any allocations.

Returns true if that’s the case.

Checks self only contains valid references to base. There will not occur any error from apply functions if this returns true.

Returns true if that’s the case.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.