Question about error in CheckTraits #4
-
I've worked though the book and I came across something in the debugging section that isn't so clear to me. The purpose of the CheckTrait is to convert a lazy trait bounds check at runtime into an eager check at compile time. Okay, I understand that. Now, the example code (condensed): #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Person {
pub first_name: String,
pub last_name: String,
}
// Static check that statically verifies all dependencies are present in the callsite.
#[allow(dead_code)] // Somehow clippy doesn't see its usage below.
pub trait CanUsePerson:
Sized + Serialize + for<'a> Deserialize<'a> + Debug + CanFormatToString + CanParseFromString
{
}
// Blanket implementation of check trait ensures the compiler enforces all checks.
impl CanUsePerson for Person {}
pub struct PersonComponents;
impl HasComponents for Person {
type Components = PersonComponents;
}
delegate_components! {
PersonComponents {
StringFormatterComponent: FormatAsJsonString,
StringParserComponent: ParseFromJsonString,
}
}
// This is called from main.rs
pub(crate) fn test_dep_check() {
let person = Person {
first_name: "John".into(),
last_name: "Smith".into(),
};
let person_str = r#"{"first_name":"John","last_name":"Smith"}"#;
assert_eq!(person.format_to_string().unwrap(), person_str);
assert_eq!(Person::parse_from_string(person_str).unwrap(), person);
} When I remove Serialize or any of the other derived traits checked in the CheckTrait, run cargo build in a terminal, it actually compiles... Although, my IDE highlights the error correctly, so there is that. Even when I remove the CanParseFromString trait import, Cargo build compiles, but then fails to run meaning the lazy checking is still happening. Isn't this supposed to trigger a compiler error? Or do I have to import the CheckTrait into main? Anything else I am missing here? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Full code from the example above: https://github.com/marvin-hansen/cgp-examples/blob/main/examples/11-debugging/dep_check.rs |
Beta Was this translation helpful? Give feedback.
-
From your linked code, it looks like you have two separate Sometimes it is useful to Ctrl+click the type in Rust Analyzer, and check that you are importing the correct type. |
Beta Was this translation helpful? Give feedback.
-
Thank you @soareschen That is correct. After having moved the error_check example into a separate folder, I suspect, Cargo does not rebuild per target the way Bazel does and that means, because I have previously build the debug_error example, everything else was linked against the previous build. In any case, after having separated the two examples into two different folders, the error handling occurs as expected. Thank you. |
Beta Was this translation helpful? Give feedback.
From your linked code, it looks like you have two separate
Person
contexts defined indep_check.rs
anddep_error.rs
. It might be an issue that thePerson
type you are checking is different from thePerson
type that cause the error.Sometimes it is useful to Ctrl+click the type in Rust Analyzer, and check that you are importing the correct type.