文档列表见:Rust 移动端跨平台复杂图形渲染项目开发系列总结(目录)
Cargo用于组织Rust项目,比直接用rustc编译多个源文件更方便。本文档介绍我们开发过程中用到的Cargo功能与小技巧。完整信息可阅读The Cargo Book。
Cargo使用依赖项目的指定git commit
rev表示要使用的commit id,可简写成前7个字符,因为git commit id前7个字符可算出完整的41个字符值。
[dependencies]
gfx-hal = { version = "0.1.0", git = "https://github.com/gfx-rs/gfx", rev = "bd7f058efe78d7626a1cc6fbc0c1d3702fb6d2e7" }
// 或者写成多行
[dependencies.gfx-hal]
git = "https://github.com/gfx-rs/gfx"
version = "0.1.0"
rev = "bd7f058efe78d7626a1cc6fbc0c1d3702fb6d2e7"
复制代码
git仓库地址需要支持https访问,如果是http需要额外配置,比较麻烦。
引用本地Rust项目
[dependencies]
hello_utils = { path = "hello_utils", version = "0.1.0" }
复制代码
path是相对于本项目Cargo.toml文件的被依赖项目的Cargo.toml的位置,填错将找不到文件,且编译报错。详细信息参考The Cargo Book/specifying-dependencies
测试Rust代码中的Markdown代码段
/// Open the physical device with `count` queues from some active queue family. The family is
/// the first that both provides the capability `C`, supports at least `count` queues, and for
/// which `selector` returns true.
///
/// # Examples
///
/// ```no_run
/// # extern crate gfx_backend_empty as empty;
/// # extern crate gfx_hal as hal;
/// use hal::General;
/// # fn main() {
///
/// # let mut adapter: hal::Adapter<empty::Backend> = return;
/// let (device, queues) = adapter.open_with::<_, General>(1, |_| true).unwrap();
/// # }
/// ```
///
/// # Return
///
/// Returns the same errors as `open` and `InitializationFailed` if no suitable
/// queue family could be found.
pub fn open_with<F, C>(
&self,
count: usize,
selector: F,
) -> Result<(B::Device, QueueGroup<B, C>), DeviceCreationError>
where
F: Fn(&B::QueueFamily) -> bool,
C: Capability,
{
use queue::QueueFamily;
let requested_family = self
.queue_families
.iter()
.filter(|family| {
C::supported_by(family.queue_type())
&& selector(&family)
&& count <= family.max_queues()
})
.next();
let priorities = vec![1.0; count];
let (id, families) = match requested_family {
Some(family) => (family.id(), [(family, priorities.as_slice())]),
_ => return Err(DeviceCreationError::InitializationFailed),
};
let Gpu { device, mut queues } = self.physical_device.open(&families)?;
Ok((device, queues.take(id).unwrap()))
}
复制代码
前面Examples
代码可以用rust-skeptic进行测试,具体做法可阅读rust-skeptic文档。