Hone logo
Hone
Problems

Coherence Checker for Rust Traits

Coherence checking is a crucial aspect of Rust's type system, ensuring that when multiple traits define the same method name, the compiler can unambiguously resolve which implementation to use. This challenge asks you to implement a simplified coherence checker that verifies if a given set of trait implementations is coherent for a specific method. This is a simplified version, focusing on the core logic without the full complexity of the Rust compiler.

Problem Description

You are tasked with building a function is_coherent that determines if a set of trait implementations is coherent for a given method signature. The input will be a vector of TraitImplementation structs, each representing a trait and its implementation of a specific method. The function should return true if the implementations are coherent (meaning there's only one implementation for the method, or the compiler can resolve the ambiguity), and false otherwise.

Key Requirements:

  • TraitImplementation: A struct representing a trait and its implementation of a method. It contains the trait name (String) and the method name (String).
  • Coherence: A set of trait implementations is coherent if, for a given method name, there is either zero or one implementation. If there are multiple implementations, the coherence checker must determine if the compiler could resolve the ambiguity (in this simplified version, we'll assume coherence if there's only one implementation).
  • Ambiguity Resolution (Simplified): For the purpose of this challenge, we will not implement full ambiguity resolution logic. If multiple traits implement the same method, the function should return false. The focus is on detecting the presence of multiple implementations.

Expected Behavior:

The is_coherent function should iterate through the provided TraitImplementation structs and check for multiple implementations of the same method. If it finds more than one implementation for a given method, it should immediately return false. If it processes all implementations without finding any ambiguity, it should return true.

Edge Cases to Consider:

  • Empty input vector: Should return true (no implementations, so inherently coherent).
  • Multiple traits implementing the same method: Should return false.
  • Different traits implementing different methods: Should return true.
  • Case sensitivity of trait and method names: Treat them as case-sensitive.

Examples

Example 1:

Input: vec![
    TraitImplementation { trait_name: "TraitA".to_string(), method_name: "foo".to_string() },
    TraitImplementation { trait_name: "TraitB".to_string(), method_name: "bar".to_string() },
]
Output: true
Explanation: There are no conflicting method implementations.

Example 2:

Input: vec![
    TraitImplementation { trait_name: "TraitA".to_string(), method_name: "foo".to_string() },
    TraitImplementation { trait_name: "TraitB".to_string(), method_name: "foo".to_string() },
]
Output: false
Explanation: Both TraitA and TraitB implement the "foo" method, creating ambiguity.

Example 3:

Input: vec![]
Output: true
Explanation: No traits are implemented, so it's coherent.

Constraints

  • The input vector TraitImplementations can contain up to 100 TraitImplementation structs.
  • trait_name and method_name strings can be up to 50 characters long.
  • The function must return a boolean value indicating coherence.
  • Performance: The function should complete within 100ms for the maximum input size.

Notes

  • Consider using a HashMap to efficiently track method implementations and their associated traits.
  • The focus is on detecting conflicts, not resolving them. Don't attempt to implement complex ambiguity resolution logic.
  • The TraitImplementation struct is provided for you.
  • Error handling is not required for this challenge. Assume the input is always valid.
#[derive(Debug, PartialEq, Eq)]
struct TraitImplementation {
    trait_name: String,
    method_name: String,
}

fn is_coherent(trait_implementations: Vec<TraitImplementation>) -> bool {
    // Your code here
}
Loading editor...
rust