小编tre*_*tcl的帖子

使用 rustc 和 clang 运行 LLVM 文件

我正在尝试使用 clang 运行.ll文件并收到链接器错误。我有一个文件test.rs,只包含一个带有println!语句的主函数。我使用命令生成 LLVM IR rustc --emit=llvm-ir --crate-type=bin test.rs。当我尝试运行输出test.ll文件时clang test.ll,出现链接器错误:

Undefined symbols for architecture x86_64:
  "std::io::stdio::_print::h178318b95760562a", referenced from:
      rust_test::main::h84a9713c734a1b45 in rust_test-9ea667.o
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

所以我认为 clang 找不到 Rust 库,并尝试使用命令提供它们的路径clang -L$HOME/.rustup/toolchains/<arch>/lib test.ll,但我收到相同的错误。


这里的目标是在 Rust 中创建几个函数,我将从 LLVM 调用它们,因此我将有一个自定义函数file.ll,它将使用 Rust LLVM IR …

llvm llvm-clang rust

5
推荐指数
0
解决办法
633
查看次数

如何基于功能标志有条件地执行模块级doctest?

我正在编写模块的文档,该模块具有一些由Cargo功能标记控制的选项。我希望始终显示此文档,以便板条箱的使用者知道它可用,但是我仅在启用此功能时运行示例。

//! This crate has common utility functions
//!
//! ```
//! assert_eq!(2, featureful::add_one(1));
//! ```
//!
//! You may also want to use the feature flag `solve_halting_problem`:
//!
//! ```
//! assert!(featureful::is_p_equal_to_np());
//! ```

pub fn add_one(a: i32) -> i32 {
    a + 1
}

#[cfg(feature = "solve_halting_problem")]
pub fn is_p_equal_to_np() -> bool {
    true
}
Run Code Online (Sandbox Code Playgroud)

货代

[package]
name = "featureful"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]

[features]
solve_halting_problem = []

[dependencies]
Run Code Online (Sandbox Code Playgroud)

在启用该功能的情况下运行,将按预期运行两个doctest:

//! This crate …
Run Code Online (Sandbox Code Playgroud)

documentation automated-tests conditional-compilation rust

5
推荐指数
1
解决办法
286
查看次数

嵌入式编译器替换找不到标准板条箱

我正在尝试替换嵌入式编译器。这是我的源代码。

#![feature(rustc_private)]
#![feature(link_args)]

extern crate rustc_driver;

fn main() {
    rustc_driver::set_sigpipe_handler();
    rustc_driver::main();
}
Run Code Online (Sandbox Code Playgroud)

这实际上是rustc源代码的精确副本。我使用环境变量构建,安装和导出了此工具。

cargo install
export RUSTC=tool1    # `tool1` is name of binary
Run Code Online (Sandbox Code Playgroud)

我试图建立另一个项目example1。这是的源代码example1

fn main() {}
Run Code Online (Sandbox Code Playgroud)

构建因错误而失败。

error[E0463]: can't find crate for `std`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: Could not compile `foo2`.

To learn more, run the command again with --verbose.
Run Code Online (Sandbox Code Playgroud)

我确认example1建造正常cargo。它只被打破tool1。(export …

rust rust-cargo

5
推荐指数
1
解决办法
264
查看次数

当关联类型没有大小时,如何避免需要`std :: marker :: Sized`?

背景

我有一种情况,我想抽象两种不同的操作模式SparseDense.我选择哪一个是编译时决定.

与这些模式正交我有很多Kernels.内核的实现细节和签名在两种模式之间不同,但每种模式具有相同的内核.内核将在运行时根据模型文件确定.

我现在想创建一个BlackBox处理模式和内核的方法.

简化代码

我删除了其他内核和稀疏模式.

pub struct XKernel;

pub trait KernelDense {
    fn compute_dense(&self, vectors: &[f32]);
}

impl KernelDense for XKernel {
    fn compute_dense(&self, vectors: &[f32]) {}
}

pub trait KernelCompute<V> {
    fn just_compute_it(&self, vectors: &[V]);
}

impl KernelCompute<f32> for (dyn KernelDense + 'static) {
    fn just_compute_it(&self, v: &[f32]) {
        self.compute_dense(v);
    }
}

pub trait Generalization {
    type V: 'static;

    type OperatorType: KernelCompute<Self::V>;

    fn set_kernel(&self, x: Box<Self::OperatorType>);

    fn compute(&self, v: &[Self::V]); …
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
1
解决办法
906
查看次数

如何为包含可为空函数指针的 FFI 创建结构?

我有一个加载共享库插件的现有 C 程序。主 C 程序通过包含整数、字符串、函数指针等的 C 结构与这些插件交互。如何从 Rust 创建这样的插件?

请注意,(真实的)C 程序不能更改,API 也不能更改,那些是固定的、现有的东西,所以这不是关于“如何最好地支持 Rust 插件”的问题,而是 Rust 如何制作*.so文件与现有的 C 程序互操作。

下面是一个 C 程序 + C 插件的简化示例:

/* gcc -g -Wall test.c -o test -ldl
   ./test ./test-api.so
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <dlfcn.h>

struct api {
  uint64_t i64;
  int i;
  const char *name;                /* can be NULL */
  void (*load) (void);             /* must not be NULL */
  void (*hello) (const char *str); /* can be NULL …
Run Code Online (Sandbox Code Playgroud)

c rust

5
推荐指数
1
解决办法
1530
查看次数

如何将 Arc&lt;T&gt; 与 T 进行比较?

我试图Arc更像其基础数据一样对待,特别是我试图使用重载运算符。以下代码给出了错误,但这是我的尝试。

use std::cmp::Ordering;
use std::option::Option;
use std::sync::Arc;

pub struct SomeNum {
    num: u32,
}

impl std::cmp::PartialEq<u32> for SomeNum {
    fn eq(&self, other: &u32) -> bool {
        return *other == self.num;
    }
}

impl std::cmp::PartialOrd<u32> for SomeNum {
    fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
        if (self.num < *other) {
            return Option::Some(Ordering::Less);
        }
        if (self.num > *other) {
            return Option::Some(Ordering::Greater);
        }
        return Option::Some(Ordering::Equal);
    }
}

fn main() {
    let test_num = SomeNum { num: 16 };
    let …
Run Code Online (Sandbox Code Playgroud)

operator-overloading rust

5
推荐指数
1
解决办法
1296
查看次数

如何运行特定模块下的所有测试功能?

假设有一个模块,下面有多个测试,如下所示。如何运行该模块下的所有测试而不运行其他模块?

#[cfg(test)]
mod cool_tests {
   #[test]
   fn first_test() {
      // Test Code
   }

   #[test]
   fn second_test() {
      // Test Code
   }
}
Run Code Online (Sandbox Code Playgroud)

rust rust-cargo

5
推荐指数
1
解决办法
2885
查看次数

将数据移动到 Rc/Arc 是否总是将其从堆栈复制到堆?

看下面这个简单的例子:

use std::rc::Rc;

struct MyStruct {
    a: i8,
}

fn main() {
    let mut my_struct = MyStruct { a: 0 };
    my_struct.a = 5;
    let my_struct_rc = Rc::new(my_struct);

    println!("my_struct_rc.a = {}", my_struct_rc.a);
}
Run Code Online (Sandbox Code Playgroud)

的官方文档Rc说:

该类型Rc<T>提供T了在堆中分配的 type 值的共享所有权。

理论上是清楚的。但是,首先my_struct不是立即包装成Rc,其次MyStruct是一个非常简单的类型。我可以在这里看到 2 个场景。

  1. my_struct被移入Rc内存内容时,它会从堆栈中复制到堆中。
  2. 编译器能够解决my_struct将移入 的问题Rc,因此它从一开始就将其放在堆中。

如果数字 1 为真,那么可能存在一个隐藏的性能瓶颈,因为在阅读代码时没有明确看到内存被复制(我假设MyStruct要复杂得多)。

如果数字 2 为真,我想知道编译器是否总是能够解决这些问题。提供的示例非常简单,但我可以想象它my_struct要复杂得多,并且在移动到Rc.

rust

5
推荐指数
1
解决办法
91
查看次数

在Rust中结束可变借用有哪些选择?

我正在努力与借阅检查员 - 不知道为什么.

虽然我通过添加一个闭包找到了一个解决方案,但我很好奇是否有其他方法可以结束可变借用,因此下一个语句可以在之后访问绑定.

这是我到目前为止所做的:

let mut canvas: Canvas = Canvas {
    width: 5,
    height: 5,
    array: vec!['x'; 5*5],
};

{
    let mut renderer: CanvasRenderer = CanvasRenderer::new(&mut canvas);

    renderer.render_point('x', 3, 3);
}

println!("The Value in the array is: {}", canvas.array[9]);
Run Code Online (Sandbox Code Playgroud)

我在一个CanvasRenderer对象的绑定周围包裹了一个闭包,在改变了画布并且范围结束之后,可以读取CanvasRenderer模具和我的可变借用canvas或其他任何东西.

这有效 - 但现在我想看到其他解决方案!

我听说过,drop(stuff)但它没有按照我的想法行事.

rust borrow-checker

4
推荐指数
1
解决办法
2018
查看次数

为什么在没有Deref胁迫其中一个字符串的情况下,不能在Rust中连接两个字符串?

这样可行:

let hello = "Hello ".to_string();
let world = "world!";

let hello_world = hello + world;
Run Code Online (Sandbox Code Playgroud)

但这不是:

let hello = "Hello ".to_string();
let world = "world!".to_string();

let hello_world = hello + world;
Run Code Online (Sandbox Code Playgroud)

但这样做:

let hello = "Hello ".to_string();
let world = "world!".to_string();

let hello_world = hello + &world;
Run Code Online (Sandbox Code Playgroud)

那是因为String需要一个指向String第二个原始的指针String吗?如果是这样,为什么?

rust

4
推荐指数
1
解决办法
587
查看次数