kornelski / imgref

A trivial Rust struct for interchange of pixel buffers with width, height & stride
https://lib.rs/crates/imgref
Apache License 2.0
59 stars 6 forks source link

How can I implement a trait for `imgref::Img<std::vec::Vec<i32>>` #13

Open hyousefGopher opened 4 years ago

hyousefGopher commented 4 years ago

I'm trying to use inline-python to get a pic from OpenCV camera, then manipulate this image with Rust, so I wrote the below code that catch the photo correctly, but once I try to handle it in Rust using imgref I get the below error:

error[E0277]: the trait bound `imgref::Img<std::vec::Vec<i32>>: pyo3::pyclass::PyClass` is not satisfied
  --> src\main.rs:44:44
   |
44 |                 let img: Img<Vec<i32>> = c.get("frame");
   |                                            ^^^ the trait `pyo3::pyclass::PyClass` is not implemented for `imgref::Img<std::vec::Vec<i32>>`
   |
   = note: required because of the requirements on the impl of `for<'p> pyo3::conversion::FromPyObject<'p>` for `imgref::Img<std::vec::Vec<i32>>`

The full code is:

#![feature(proc_macro_hygiene)]
use inline_python::*;

use imgref::*;
use web_view::*;

fn main() {
    let c = Context::new();

    web_view::builder()
    .title("Call open CV")
    .content(Content::Html(HTML))
    .size(800, 600)
    .user_data(())
    .invoke_handler(|_, arg| {
        match arg {
            "open" => {
                c.run(python!  {
                            import cv2
                            x = 2
                            cap = cv2.VideoCapture(0)
                            ret, frame = cap.read()
                            cv2.imshow('frame', frame)
                            cv2.waitKey(0)
                    })
            },
            "close" => {

                let img: Img<Vec<i32>> = c.get("frame");

            },
            _ => (),
        }
        Ok(())
    }).run()
    .unwrap();
}

const HTML: &str  = r#"
<!doctype html>
<html>
    <body>
        <button onclick="external.invoke('open')">Run</button>
        <button onclick="external.invoke('close')">Close</button><br>
        <br>

    </body>
</html>
"#;

Can you help, Thanks.

kornelski commented 4 years ago

Unfortunately, you can't. Rust's "orphan rules" roughly say that you can either implement your trait for any type, or any trait for your type.

You can't implement someone else's trait for someone else's type.

You can wrap Img in a newtype (with Deref for ease of use).