Stream: beginners

Topic: ✔ Rust Platform function that takes a RocList<RocStr>


view this post on Zulip Rob (Nov 08 2022 at 14:46):

Hello, I'm trying to add an effect that executes a Command using Rust platform code. I got it working with just the command arg. But I wanted to pass in a list of args. But I keep getting a seg fault. My Effect definition is
exec : Str, List Str -> Effect (Result Str {})
and my Rust code is
pub extern "C" fn roc_fx_exec(cmd: &RocStr, args: RocList<RocStr>) -> RocResult<RocStr, ()> {

If I remove the args param from the code it compiles and runs fine. But with the RocList it seg faults.

view this post on Zulip Brendan Hansknecht (Nov 08 2022 at 16:14):

My first gut reaction is that it is related to borrowing vs not borrowing(aka pass by reference or not). Should the RocList be &RocList?

view this post on Zulip Brendan Hansknecht (Nov 08 2022 at 16:15):

Just seems strange that the string is passed by reference, but not the list.

view this post on Zulip Folkert de Vries (Nov 08 2022 at 16:24):

no that is what the llvm codegen does at the moment

view this post on Zulip Folkert de Vries (Nov 08 2022 at 16:24):

at least, internally

view this post on Zulip Folkert de Vries (Nov 08 2022 at 16:24):

hmm the C interface might do something different. Worth a try

view this post on Zulip Rob (Nov 08 2022 at 17:39):

@Brendan Hansknecht that worked thanks, but now I'm getting a seg fault trying to change the &RocList<&RocStr> to a Vec<String>
Here's the code I'm trying:
let rust_args: Vec<String> = args.as_slice().to_vec().iter().map(|x| x.as_str().to_string()).collect();

view this post on Zulip Brendan Hansknecht (Nov 08 2022 at 17:57):

I think it should be &RocList<RocStr>

view this post on Zulip Rob (Nov 08 2022 at 18:27):

@Brendan Hansknecht that did it! Thanks so much!

view this post on Zulip Notification Bot (Nov 08 2022 at 18:27):

Rob has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC