gpt4 book ai didi

rust - 使用核心图形时,没有为类型 CGContext 找到名为 wrap_under_create_rule 的关联项

转载 作者:行者123 更新时间:2023-11-29 08:33:10 26 4
gpt4 key购买 nike

我试图通过 core-graphics crate 在 Mac 应用程序中使用来自 Rust 的 Quartz2D,但我在使用 core-graphics 时遇到了问题。我可以通过以下方式获取当前上下文的 CGContextRef:

let cg_context_ref: CGContextRef = unsafe {
let ns_graphics_context: *mut Object = msg_send![Class::get("NSGraphicsContext").unwrap(), currentContext];
msg_send![ns_graphics_context, CGContext]
}

然后,我尝试使用 wrap_under_create_ruleCGContextRef 构造一个 CGContext , 来自pub trait TCFType from core-foundation :

let gc: CGContext = unsafe {
CGContext::wrap_under_create_rule(unsafe {
let ns_graphics_context: *mut Object = msg_send![Class::get("NSGraphicsContext").unwrap(), currentContext];
msg_send![ns_graphics_context, CGContext]
})
};

但是编译报错:

error: no associated item named `wrap_under_create_rule` found for type `core_graphics::context::CGContext` in the current scope
--> src/lib.rs:45:9
|
45 | CGContext::wrap_under_create_rule(unsafe {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
= help: candidate #1: `use core_foundation::base::TCFType;`

我已经有 use core_foundation::base::TCFType;,不过在相同的范围内。它看起来像这样:

#[macro_use]
extern crate objc;
extern crate core_foundation;
extern crate core_graphics;

use objc::runtime::{Object, Class};
use core_foundation::base::TCFType;
use core_graphics::context::CGContext;

fn main() {
let gc: CGContext = unsafe {
CGContext::wrap_under_create_rule(unsafe {
let ns_graphics_context: *mut Object = msg_send![Class::get("NSGraphicsContext").unwrap(), currentContext];
msg_send![ns_graphics_context, CGContext]
})
};
}

我有点 Rust 菜鸟,所以我被困在这里。为什么这不起作用,我怎样才能让它起作用?修改 core-graphics 是一个选项,因为无论如何我以后都必须这样做。

最佳答案

很有可能您正在处理两个不同版本的 TCFType。按照你的例子,我看到了同样的错误。然后我跑了cargo-tree :

$ cargo tree
cg v0.1.0 (file:///private/tmp/cg)
├── core-foundation v0.4.0 <---- here
│ ├── core-foundation-sys v0.4.0
│ │ └── libc v0.2.23
│ └── libc v0.2.23 (*)
├── core-graphics v0.8.1
│ ├── bitflags v0.8.2
│ ├── core-foundation v0.3.0 <---- here
│ │ ├── core-foundation-sys v0.3.1
│ │ │ └── libc v0.2.23 (*)
│ │ └── libc v0.2.23 (*)
│ └── libc v0.2.23 (*)
└── objc v0.2.2
└── malloc_buf v0.0.6
└── libc v0.2.23 (*)

从这里,我可以看出 core-graphics 依赖于 core-foundation 0.3.0,但是你的 crate 依赖于 core-foundation 0.4.0。

两个不同版本的 crates 提供的类型不一样,编译器告诉你的。它只是没有告诉您版本不同。

要修复它,请将您的 core-foundation 版本限制为 0.3.0。这样做会导致此错误消失,但随后出现了一些以前隐藏的不相关错误。

关于rust - 使用核心图形时,没有为类型 CGContext 找到名为 wrap_under_create_rule 的关联项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44360007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com