我試圖在Rust應用程序中通過核心圖形箱使用Quartz2D,但我無法使用核心圖形。我能獲得CGContextRef
與當前環境:當使用core-graphics時,沒有找到類型爲CGContext的關聯項目wrap_under_create_rule
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_rule
構建從CGContextRef
一個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]
})
};
}
我有點生鏽小白,所以我被困在這裏。爲什麼不能這樣工作,我怎樣才能使它工作?修改核心圖形是一種選擇,因爲無論如何,我將不得不在晚些時候完成它。