Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting between ArcOSMObj and StringOSMObj? #8

Closed
erictapen opened this issue Nov 17, 2021 · 1 comment
Closed

Casting between ArcOSMObj and StringOSMObj? #8

erictapen opened this issue Nov 17, 2021 · 1 comment

Comments

@erictapen
Copy link

Thanks for this great piece of software.

I'm currently struggling with converting StringOSMObj to an ArcOSMObj. My intuition is that it shouldn't be a problem, as I already was able to stream objects from an PBFReader to an XMLWriter. And of course there is the OSMObj trait.

My problem boils down to this: I want to write a test for a function show_id that in production only processes ArcOSMObj from an PBFReader. However for the test I'd like to be able to write xml, as it is more convenient. I have no problem with putting the trait OSMObj into show_id's type signature instead of ArcOSMObj.

However this doesn't compile:

use osmio::{OSMObj, ObjId};

fn show_id(obj: dyn OSMObj) -> ObjId {
    obj.id()
}

fn main() {
    println!("Hello, world!");
}

#[cfg(test)]
mod tests {
    use super::*;
    use osmio::xml::XMLReader;
    use osmio::OSMReader;

    #[test]
    fn test_show_version() {
        let xml = r#"
          <?xml version="1.0" encoding="utf-8"?>
          <osm version="0.6">
            <node id="1" lat="0.0" lon="0.0"/>
          </osm>
        "#;
        let reader = XMLReader::new(xml.as_bytes());
        for obj in reader.objects() {
            assert_eq!(1, show_id(obj));
        }
    }
}
$ rustc --version
rustc 1.55.0

$ cargo test
   Compiling cast_test v0.1.0 (/home/kerstin/fh/cityvis/camps/osm-history/cast_test)
error[E0038]: the trait `OSMObj` cannot be made into an object
   --> src/main.rs:3:17
    |
3   | fn show_id(obj: dyn OSMObj) -> ObjId {
    |                 ^^^^^^^^^^ `OSMObj` cannot be made into an object
    |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
   --> /home/kerstin/.cargo/registry/src/github.com-1ecc6299db9ec823/osmio-0.6.0/src/lib.rs:285:23
    |
285 | pub trait OSMObjBase: PartialEq + Debug + Clone {
    |                       ^^^^^^^^^ the trait cannot be made into an object because it uses `Self` as a type parameter

For more information about this error, try `rustc --explain E0038`.
error: could not compile `cast_test` due to previous error

Any ideas on how to achieve this?

@erictapen
Copy link
Author

Nevermind, figured that my problem was something entirely different. The test case above was solved with

//              v
fn show_id(obj: impl OSMObj) -> ObjId {
    obj.id()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant