Skip to main content

negotiate

Function negotiate 

Source
pub fn negotiate(theirs_min: u32, theirs_max: u32) -> Option<u32>
Expand description

The version both sides speak: the highest in the intersection of their ranges, or None when the ranges do not meet.

Taking the highest common version is what lets two executables of different ages link without either being told about the other: each states the range it speaks and the newest both understand wins. An inverted range is no range, so a peer that states nonsense is refused rather than accommodated.

use majordomus_cli::mesh::link::{negotiate, LINK_PROTOCOL_MAX, LINK_PROTOCOL_MIN};

assert_eq!(negotiate(LINK_PROTOCOL_MIN, LINK_PROTOCOL_MAX), Some(LINK_PROTOCOL_MAX));
assert_eq!(negotiate(0, 99), Some(LINK_PROTOCOL_MAX), "the newest both speak");
assert_eq!(negotiate(LINK_PROTOCOL_MAX + 1, LINK_PROTOCOL_MAX + 3), None, "no overlap");
assert_eq!(negotiate(3, 2), None, "an inverted range is no range");