折腾笔记[22]-解决rust编译离奇报错

摘要

解决rust代码编译时的离奇报错.

描述

项目中使用了ort库和ndarray库.

ort库的依赖文件为:

[package]
name = "ort"
description = "A safe Rust wrapper for ONNX Runtime 1.21 - Optimize and accelerate machine learning inference & training"
version = "2.0.0-rc.9"
edition = "2021"
rust-version = "1.81"
[dependicies]
ndarray = { version = "0.16.1", default-features = false, optional = true }

项目的依赖文件:

[workspace.package]
edition = "2024"
rust-version = "1.85"
# 线性代数
ndarray = { version = "0.16.1", path = "./static/ndarray" }
# onnxruntime(C绑定), 默认特性
ort = { version = "2.0.0-rc.9", path = "./static/ort" }

编译报错:

error[E0277]: the trait bound `ArrayBase<OwnedRepr<f32>, Dim<[usize; 4]>>: OwnedTensorArrayData<_>` is not satisfied
--> crates/seekslam_examples/examples/ort_segment.rs:181:58
 |
181 |         let input_tensor = ort::value::Value::from_array(input_array.clone())?;
 |                            ----------------------------- ^^^^^^^^^^^^^^^^^^^ the trait `OwnedTensorArrayData<_>` is not implemented for `ArrayBase<OwnedRepr<f32>, Dim<[usize; 4]>>`
 |                            |
 |                            required by a bound introduced by this call
 |
 = help: the following other types implement trait `OwnedTensorArrayData<I>`:
           (D, Box<[T]>)
           (D, Vec<T>)
           ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<T>, D>

库的版本都是一致的, 报错非常离奇, 其实ArrayBase<OwnedRepr<f32>, Dim<[usize; 4]>>就是需要的ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<T>, D>特征, 非常奇怪.

解决方法

使用patch方法替换ort库使用的ndarray库:
项目的Cargo.toml文件

[patch.crates-io]
# 替换依赖文件
ndarray = { version = "0.16.1", path = "./static/ndarray" }

这样所有依赖ndarray版本号"0.16.1"都会使用相同的库代码, 就不会产生签名不一致问题了.

提示

下载依赖可以使用cargo vendor命令.

posted @ 2025-04-12 10:16  qsBye  阅读(39)  评论(0)    收藏  举报