有没有一种好方法将外部资源数据包含到 Rust 源代码中? [英] Is there a good way to include external resource data into Rust source code?

查看:33
本文介绍了有没有一种好方法将外部资源数据包含到 Rust 源代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象下面的例子:

let SHADER: &'static str = "
#version 140

attribute vec2 v_coord;
uniform sampler2D fbo_texture;
varying vec2 f_texcoord;

void main(void) {
    gl_Position = vec4(v_coord, 0.0, 1.0);
    f_texcoord = (v_coord + 1.0) / 2.0;
}";

fn main() {
    // compile and use SHADER
}

当然,您可以如上所示编写内联着色器,但是在使用外部软件设计着色器或具有多个着色器时,这会变得非常复杂.您也可以从外部文件加载数据,但有时您只想提供一个小的可执行文件,而无需弄清楚资源的存储位置.

Of course you can write the shader inline as shown above, but this gets really complicated when designing shaders using external software or when having multiple shaders. You can also load the data from external files, but sometimes you only want to provide a single small executable without the need to figure out where the resources are stored.

如果该解决方案也适用于二进制文件(例如图标、字体),那就太好了.

It would be great if the solution also works for binary files (e.g. icons, fonts).

我知道可以编写 rustc 插件,据我所知应该可以提供这样的功能,但是编写我自己的插件相当复杂,我想知道是否已经有一个好的插件/lib/standard 包含资源文件的方式.另一点是它应该在不利用手动链接器+指针方式的情况下工作.

I know that it is possible to write rustc plugins and as far as I understand it should be possible to provide such a feature, but writing my own plugin is rather complicated and I would like to know if there is already a good plugin/lib/standard way to include resource files. Another point is that it should work without exploiting the manual linker+pointer way.

推荐答案

我相信您正在寻找 include_str!() 宏:

I believe you are looking for the include_str!() macro:

static SHADER: &'static str = include_str!("shader.glsl");

shader.glsl 文件应该位于源文件旁边,这样才能工作.

shader.glsl file should be located right beside the source file for this to work.

还有 include_bytes!() 对于非 UTF-8 数据:

There's also include_bytes!() for non-UTF-8 data:

static SHADER: &'static [u8] = include_bytes!("main.rs");

<小时>

不要将这些与 include!<混为一谈/a>,它导入一个文件作为 Rust 代码.

这篇关于有没有一种好方法将外部资源数据包含到 Rust 源代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆