- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以解释吗?如何使用“ccall
”函数将任何数组从 julia 发送到 rust。发送常用变量或常量没有问题。
我有 Julia
代码:
A = Array{Float64,1}(undef, 2)
print("A is ",A, "\n")
ccall((:recvstruct, "target/debug/liblib"),Float64, (Ref{Array{Float64,1}}, Int32,), A, sizeof(A))
和
Rust
代码
#[no_mangle]
pub extern fn recvstruct(vec: &mut Vec<f64> , len: usize){
println!("reiceved?: {:?}", len);
println!("reiceved?: {:?}", vec);
}
输出是:
A is [0.0, 0.0]
Hello from cargo
reiceved?: 16
和奇怪的无尽数组:
, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000069329110091509, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291559949425, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291100915405, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291559949425, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291100915405, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291559949425, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291100915563, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291559949425, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291100915563, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291559949425, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291100915563, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000693291559949425, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000069329110091572, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Julia 安装:
Version 1.5.2
rust ( cargo ):
cargo 1.49.0 (d00d64df9 2020-12-05)
最佳答案
Julia 侧
在 (argtype1, ...)
的 ccall
声明 recvstruct
的第一个参数的类型作为指向 Ptr{T}
的内存地址的指针.
直接来自 ccall docs
:
Each argvalue to the ccall will be converted to the corresponding argtype, by automatic insertion of calls to unsafe_convert(argtype, cconvert(argtype, argvalue))
a = [2.0, 3.0]
result = ccall((:recvstruct, "target/debug/liblib"), Float64, (Ptr{Float64}, UInt,), a, length(a))
println("got: $result")
rust 面
recvstruct
的第一个参数作为指向
float64
数组的内存指针.
slice::from_raw_parts
用于获取值的一部分。
#[no_mangle]
pub extern "C" fn recvstruct(array_ptr: *const f64, len: usize) -> f64 {
_recvstruct(unsafe {
std::slice::from_raw_parts(array_ptr as *const f64, len)
}, len)
}
fn _recvstruct(vec: &[f64] , len: usize) -> f64 {
match len {
2 => vec[0] * vec[1],
_ => 0.0
}
}
请注意
from_raw_parts
被标记为不安全。手册页的安全部分包含所有需要考虑的细节。
关于arrays - 将数组从 Julia 发送到 Rust(使用 "ccall"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66122348/
亲爱的 GHC/Haskell 大师, 我目前正在使用 GHC 编写一个(中型)Haskell 服务器应用程序,它(大量)通过 FFI 使用第 3 方 C 库函数。换句话说,在服务客户端请求时,服务器
我希望有人能澄清 Julia 垃圾收集器行为的一个方面,以及它如何与使用 ccall 调用 C 函数分配的内存进行交互。 . 例如,我正在调用以下电话: setup::Ptr{Void} =
我是 Julia 的新手,我试图在语言层面上理解 ccall 是。在语法级别,它看起来像一个普通函数,但它在获取参数方面的行为显然不同: Note that the argument type tup
我正在尝试使用 ccall 与 Windows 函数(具体来说是 FindWindowA)进行交互,但我遇到了一个奇怪的行为 julia> ccall((:FindWindowA, :user32),
我正在尝试使用 Julia 的 ccall与 C 库接口(interface)的函数。所有类型和指针都是正确的,并且下面的函数调用成功地返回了正确的答案(为简洁起见,此处未显示变量定义和设置)。
我发现有两个调用约定使用 GHC's FFI :ccall 和capi。该文档没有关于这两个约定的太多信息。它们之间有什么区别,什么时候应该使用它们?一个比另一个快吗? 最佳答案 ccall 是正常的
我想调用一个调用 c 函数的函数,但我希望能够换出实际的 C 函数 function Ccall(fn) ... more code 1 ... ccall((fn, libc)
有人可以解释吗?如何使用“ccall”函数将任何数组从 julia 发送到 rust。发送常用变量或常量没有问题。 我有 Julia代码: A = Array{Float64,1}(undef, 2)
我制作了一个简短的 Haskell 程序,它公开了 C 或 Python 的函数。已关注 http://www.haskell.org/ghc/docs/7.0.3/html/users_guide/
我在 c++ 中有一些回调函数,我想在使用 emscripten 编译后在 javascript 中重新创建。 有谁知道如何使用 ccall 或 cwrap 调用它们? 谢谢! 最佳答案 我使用的技术
我是一名优秀的程序员,十分优秀!