2019-06-06 17:02:47

So recently, I have been checking out Rust a bit, and I like how fast it compiles and runs, and the Cargo package manager.
The executables make pyinstaller look like a baby toy.
I'm wondering, however, if it'd be possible to make good games with it.

2019-06-06 19:32:50

Its possible. There are good game engines out there (Amethyst comes to mind) but as for audio? You've got Alto -- an OpenAL wrapper for Rust. There's rfmod, but that hasn't been maintained, to my knowledge. There's SDL2, which should do good for you in regards to input, rendering, etc. Networking.. there are lots of ways of doing this one.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-06-07 17:34:40

A semi-joking comparison of Rust and Go: https://matthias-endler.de/2017/go-vs-rust/

Rust would be my pref, but it will be so much hard work.

While I'm at it I should change from OpenGL to Vulkan.

Because Rust & Vulkan.

While I'm at it I may also pick up one of Apple's recently announced $1,430USD monitor stands.

2019-06-13 17:33:04

I'm looking for a way to build a DLL with rust.
I wanted it to export
add(f:i32, s:i32) ->i32 {
f+s
}
It's supposed to add 2 numbers together, but when I put crate-type=['cdylib'] under the [lib] section of cargo.toml, the resulting DLL only exported rust_eh_personality?
Can you please tell me what I'm doing wrong?
It seems like it'd be really cool to make DLLS with Rust rather than C.
Thanks!

2019-06-13 18:05:28

Hi,
have you specified, that it should be exported?

extern "C" fn add(a: i32, b: i32) -> i32
{
a+b
}

Means, that add method should be exported using C calling convention.

Best regards

Rastislav

2019-06-13 18:28:24

Oh! No I didn't. I had no idea about that sintax.
I did some searches on Google, and it said nothing about that.
Thanks a lot!

2019-06-13 19:26:50 (edited by Ethin 2019-06-13 19:27:44)

@5 is sort of correct.
To make a function export itself in rust, add the following before it:
#[no_mangle]
This prevents Name mangling.
Additionally, remember the difference between dylib, staticlib, and cdylib:

  • --crate-type=dylib, #[crate_type = "dylib"] - A dynamic Rust library will be produced. This is different from the lib output type in that this forces dynamic library generation. The resulting dynamic library can be used as a dependency for other libraries and/or executables. This output type will create *.so files on linux, *.dylib files on osx, and *.dll files on windows.

  • --crate-type=staticlib, #[crate_type = "staticlib"] - A static system library will be produced. This is different from other library outputs in that the compiler will never attempt to link to staticlib outputs. The purpose of this output type is to create a static library containing all of the local crate's code along with all upstream dependencies. The static library is actually a *.a archive on linux and osx and a *.lib file on windows. This format is recommended for use in situations such as linking Rust code into an existing non-Rust application because it will not have dynamic dependencies on other Rust code.

  • --crate-type=cdylib, #[crate_type = "cdylib"] - A dynamic system library will be produced. This is used when compiling a dynamic library to be loaded from another language. This output type will create *.so files on Linux, *.dylib files on macOS, and *.dll files on Windows.

(Source: https://doc.rust-lang.org/reference/linkage.html.)
So, in summary:

  • If you want a rust library DLL (which is compiler-defined), use crate-type = ["dylib"].

  • If you want something that can be called from another language, use crate-type - ["cdylib"]

  • If you want a static library, use crate-type = ["staticlib"]

Finally, in your DLL, either add:
pub
before your function, or:
pub extern "C"
If you want structures/enumerations/... visible in your DLL, add:
#[repr(C)]
Before the struct/enum/... in question.
Just a question... why are you doing this -- do you know the language sufficiently enough for this to be useful to you?

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-06-13 19:51:43 (edited by keithwipf1 2019-06-13 19:52:31)

I am trying to make a DLL that I can use from Python with ctypes.
It was sort of an experiment.
I wanted to start with an add function, then maybe make something more useful.
I did use the advice from number 5, here is the src/lib.rs file:
pub extern "C" fn add(f:i32, s:i32) ->i32 {
f+s
}
This creates a DLL, and then I ran dependency walker which can show all the exported functions. There was only 'rust_eh_personality", even if the name got mixed up, shouldn't there be something else?
My cargo file is like this
[package]
name = "dll"
version = "0.1.0"
authors = ["Keith"]
edition = "2018"

[dependencies]
[lib]
name="test"
crate-type=["cdylib"]
Thanks.
Edit: Also, this came from home, I don't have the VC build tools installed on here

2019-06-13 20:16:00

@8, you need to add #[no_mangle] before the function and before pub extern "C". I'd recommend you run dumpbin /exports on the DLL instead of using dependency walker. See post six.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-06-16 23:09:25

Also note that, the instant you move beyond basic primitives, you're going to have to start tossing around u64s representing pointers to your objects, then converting them to and from Rust structs. IOW, it's probably not a good idea to just create a DLL in Rust because it's fun, unless that's your only goal.

I.e. write your project in whatever language you're familiar with. Then, if and only if you've identified performance bottlenecks or areas in the code that you'd like extra safety on, rewrite those paths in Rust.

I've got a Rust project for which I'm trying to export C/JNI interfaces, and the overhead of that isn't huge, but it's something. Glad I wrote and significantly tested the project first before working on the FFI bridges. They're both very distinct sets of problems.

2019-06-16 23:55:16

@10, I've never passed pointers around; the most I've passed around are large structs with references.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-06-17 17:05:25

Ugh, yes. Dealing with Python bytes seems to require an array of u8 and then I just stop when I find a 0. I guess recieving unicode strings requires an array of chars.
Is there some kind of reference on how to convert between all the types, like, which types will Python convert into what, and which will just cause it to crash?
I did think if I was going to make a DLL that managed objects, I'd pass numbers from the init function and the DLL would manage the objects based on the number you passed. I don't think I came up with this idea though, big_smile

2019-06-17 20:06:00

@12, what are you exactly trying to do in Rust with this DLL? Making a DLL is complicated in any language; Rust just simplifies it a *tiny* bit.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-06-22 20:47:29

I was thinking I'd make something to simplify making games and help with things like sounds.

2019-06-22 23:19:54 (edited by Ethin 2019-06-22 23:20:14)

@14, in a DLL? That would be pretty hard to do, man. You can do it, but I'd just recommend you make your hole game in rust if your going to do that.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github