Skip to content

Commit

Permalink
impl compare for resolve result
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Oct 21, 2024
1 parent cef09b3 commit 862e75b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
37 changes: 36 additions & 1 deletion crates/libs/core/src/client/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,47 @@ impl ServiceEndpointsVersion {
Self { com }
}

/// TODO: documentation.
/// CSharp doc: Zero if this and other are equivalent,
/// a negative value if this is less than other, and a positive value if this is greater than other.
///
/// This is not usually used in CSharp apps, but the implementation is provided here for completeness.
/// Ideally one should use mssf_core::client::svc_mgmt_client::ResolvedServicePartition instead, by
/// doing an additional FabricClient resolve call to retrieve from FabricClient cache.
pub fn compare(&self, other: &ServiceEndpointsVersion) -> crate::Result<i32> {
unsafe { self.com.Compare(&other.com) }
}
}

impl PartialEq for ServiceEndpointsVersion {
fn eq(&self, other: &Self) -> bool {
match self.compare(other) {
Ok(i) => i == 0,
Err(_) => false, // error comparing different services
}
}
}

impl PartialOrd for ServiceEndpointsVersion {
/// Compare the version of the resolved result.
/// a > b means partial_cmp(a,b) == Some(Greater) i.e. a.compare_version(b) > 0.
/// a is newer and up to date.
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.compare(other) {
Ok(i) => {
if i == 0 {
Some(std::cmp::Ordering::Equal)
} else if i > 0 {
Some(std::cmp::Ordering::Greater)
} else {
Some(std::cmp::Ordering::Less)
}
}
// If you compare version of different service you get error
Err(_) => None,
}
}
}

// Bridge implementation for the notification handler to turn rust code into SF com object.
#[windows_core::implement(IFabricServiceNotificationEventHandler)]
pub struct ServiceNotificationEventHandlerBridge<T>
Expand Down
30 changes: 30 additions & 0 deletions crates/libs/core/src/client/svc_mgmt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ impl ResolvedServicePartition {
}
}

impl PartialEq for ResolvedServicePartition {
fn eq(&self, other: &Self) -> bool {
match self.compare_version(other) {
Ok(i) => i == 0,
Err(_) => false, // error comparing different services
}
}
}

impl PartialOrd for ResolvedServicePartition {
/// Compare the version of the resolved result.
/// a > b means partial_cmp(a,b) == Some(Greater) i.e. a.compare_version(b) > 0.
/// a is newer and up to date.
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.compare_version(other) {
Ok(i) => {
if i == 0 {
Some(std::cmp::Ordering::Equal)
} else if i > 0 {
Some(std::cmp::Ordering::Greater)
} else {
Some(std::cmp::Ordering::Less)
}
}
// If you compare version of different service you get error
Err(_) => None,
}
}
}

#[derive(Debug, PartialEq, Clone)]
pub enum ServiceEndpointRole {
Invalid,
Expand Down

0 comments on commit 862e75b

Please sign in to comment.