我正在学习Rust,因为我试图找到一个替代C/C++的互操作C#.
怎么可能像下面的C代码一样编写Rust代码?到目前为止,这是我的Rust代码,没有选择来编组它:
pub struct PackChar { id: u32, val_str: String, }
#[no_mangle]
pub extern fn get_packs_char(size: u32) -> Vec<PackChar> {
let mut out_vec = Vec::new();
for i in 0 .. size {
let int_0 = '0' as u32;
let last_char_val = int_0 + i % (126 - int_0);
let last_char = char::from_u32(last_char_val).unwrap();
let buffer = format!("abcdefgHi{}", last_char);
let pack_char = PackChar {
id: i,
val_str: buffer,
};
out_vec.push(pack_char);
}
out_vec
}
Run Code Online (Sandbox Code Playgroud)
上面的代码试图重现以下C代码,我能够按原样与之互操作.
void GetPacksChar(int size, PackChar** DpArrPnt)
{
int TmpStrSize …Run Code Online (Sandbox Code Playgroud) 这个问题指的是'新'D: DMD32 D编译器v2.068.2
对于TL; DR如果您不需要详细信息,请跳至下面的问题
使用visual studio(我正在使用v2010),通过创建new project- > D- >Dynamic Library
当项目creartion过程完成时,在解决方案资源管理器中有2个文件:
保持.def文件dllmain.d不变,我已经设法理解通过添加一些新功能和优先:
extern (Windows) export
Run Code Online (Sandbox Code Playgroud)
将导出该函数,它将可以调用c#,没有尝试使用C或C++.
请注意,除非您知道自己在做什么,否则请勿触摸任何现有代码.
所以下面的代码按预期工作
extern (Windows) export uint D_mathPower(uint p)
{
return p * p;
}
Run Code Online (Sandbox Code Playgroud)
使用以下签名从C#调用它:
[DllImport(@"pathTo...\DynamicLib1.dll", CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
public static extern uint D_mathPower(uint p);
Run Code Online (Sandbox Code Playgroud)
我可以很容易地使用它如下:
uint powD = D_mathPower(5);
Run Code Online (Sandbox Code Playgroud)
我如何返回一组结构(最好是最具成本效益的方式)?
struct dpack{ char* Name; uint Id; }
Run Code Online (Sandbox Code Playgroud)
我一直在使用这两种尝试char[]和char*,但没有成功.
到目前为止这是我的代码 …
我正在寻找一种最有效的方式来阅读文本文件.
考虑到所有可能的优势,例如:
代码将是平台特定的Windows操作系统
并且我正在为当前的CPU等编写一个特定的事实.
*不介意它不是多平台.
只是简单的性能问题
我怎么能以最快的方式编码,将文本文件的每一行读入结构?
说结构是:
typdef struct _FileL{
uint lidx;
char* lncontent;
} FileL;
Run Code Online (Sandbox Code Playgroud)
我想的是:
传递FileL上面的动态数组和文件的路径什么是最有效的方式来填充和返回给定文件的行集合?
getFileLines(char* fullPath, FileL** fileLines){
uint linesCount = 0;// total lines
uint curLnIndex = 0;// lines counter
FILE* srcFL; // will hold the source file using passed fullPath
// now read file into memory
//that is the only way i could think of
//to be able to assign lineCount used to calculate the array length
//and also the fastest way …Run Code Online (Sandbox Code Playgroud) c# ×2
interop ×2
algorithm ×1
c ×1
d ×1
file ×1
marshalling ×1
performance ×1
pointers ×1
rust ×1