2017-06-05 33 views
0

我試圖在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] 
     }) 
    }; 
} 

我有點生鏽小白,所以我被困在這裏。爲什麼不能這樣工作,我怎樣才能使它工作?修改核心圖形是一種選擇,因爲無論如何,我將不得不在晚些時候完成它。

回答

0

機會很好,你正在處理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 (*) 

從這個,我可以看到,核心圖形取決於核心基礎0.3.0,但你的箱子取決於核心基礎0.4.0。

由兩個不同版本的板條箱提供的類型不一樣,編譯器告訴你。它只是沒有告訴你,版本不同。

要修復它,請將您的核心基礎版本限制爲0.3.0。這樣做會導致此錯誤消失,但之前隱藏了一些無關的錯誤。

相關問題