Posts

Showing posts with the label rust

What are the rules for a rustdoc test of a method to pass?

Image
Clash Royale CLAN TAG #URR8PPP What are the rules for a rustdoc test of a method to pass? I am trying to write a rustdoc test of a method called insert . The testing function is called in the last line of the test, and when I comment it out the test passes just fine. insert Here is the (unhelpful) error message: $ cargo test ... failures: src/blindhide.rs - blindhide::BlindHide::insert (line 34) Could it be that there are special rules for a rustdoc test to pass (for example the use of assert! macro to prove that it returns a proper value)? assert! I am asking because my method passes integration test with very similar code, so I am pretty confident it's correct The test: /// # Examples /// ``` /// extern crate crypsto; /// extern crate rgb; /// /// use std::slice; /// use rgb::RGB; /// /// let msg = "This is a secret message".as_bytes(); /// let img = vec![RGB {r: 0, g: 0, b: 0}; 800*500]; /// /// // Create a reference to the bytes of img /// let p: *const Vec<RG...

How to find the type of the caller of a function in Rust?

Image
Clash Royale CLAN TAG #URR8PPP How to find the type of the caller of a function in Rust? How can I get the caller type in my function? struct A; struct B; impl A { fn new() -> Self { A } fn call_function(&self) { B::my_function(); } } impl B { pub fn my_function() { println!("Hello"); // println!("{}" type_of_the_caller) // I want to get type A here // Is it possible to get the caller type which is A in this case? } } fn main() { let a = A::new(); a.call_function(); } Here is the working code in playground. This is simplified code for an example. I guess you meant fn call_function(&self) ? Please add more precision about what you want to do with the type of the caller – Boiethios 5 hours ago fn call_function(&self) ...