Stream: beginners

Topic: Sockets on basic-cli


view this post on Zulip Agus Zubiaga (Feb 28 2023 at 00:44):

Are there any platforms out there with some basic sockets support or do I have to make my own currently?

view this post on Zulip Richard Feldman (Feb 28 2023 at 00:45):

none that I know of, but if you're interested in adding them to an existing platform (e.g. basic-cli) I'd be happy to help!

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 00:47):

That’d be fun! I’ll clone basic-cli and get comfy with it

view this post on Zulip Richard Feldman (Feb 28 2023 at 01:08):

feel free to post any questions here!

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 01:44):

Very hardcoded but it's a start! client.gif

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 01:53):

haha! I love how specific the function is.

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 01:53):

glad it wasn't to hard to figure the base out

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 01:56):

Yeah, it felt so intuitive!

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 01:58):

Now I need to return a pointer to the stream so I can read and write later. Not sure what type I need to use for that.

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 02:00):

Likely you want an opaque type that simply wraps a Nat or a Box {}

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 02:00):

That way userland Roc code won't be able to do anything with it, but it can still be passed around as a pointer.

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 02:01):

You can look at what was done here for a file: https://github.com/roc-lang/roc/blob/main/examples/cli/false-interpreter/platform/File.roc

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 02:02):

In this case, I also managed the lifetime of the file to make sure the handle got closed. Only exposing a function that took a lambda for what to do with the file.

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 02:02):

Oh, though I used U64, which is wrong on 32bit platforms, so I wouldn't copy that.

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 02:03):

I see

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 02:04):

So Nat is like usize?

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 02:04):

Yep

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 02:05):

Alright, on it! Thank you :)

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 03:58):

It's getting more interesting chat.gif

view this post on Zulip Ayaz Hafiz (Feb 28 2023 at 03:59):

This is pretty fire, nice Agus!

view this post on Zulip Brendan Hansknecht (Feb 28 2023 at 04:29):

I love the gif. Really cool to see.

view this post on Zulip Brian Carroll (Feb 28 2023 at 06:54):

Oh wow, so cool! :tada:

view this post on Zulip Richard Feldman (Feb 28 2023 at 12:55):

wow, that's awesome!!! :heart_eyes:

view this post on Zulip Richard Feldman (Feb 28 2023 at 12:55):

@Agus Zubiaga was this all on your first day getting into Platform development?

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 12:56):

I did cross midnight but yeah :grinning:

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 12:58):

It was my first day actually using Roc at all. I had only seen some talks before.

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 12:59):

Props to you folks for making it so straightforward!

view this post on Zulip Richard Feldman (Feb 28 2023 at 12:59):

wow, amazing! :heart_eyes_cat:

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 13:09):

It's been fun! I'll play with the API when I get some more free time. I have to make some things fail at the Task level instead of panicking in the platform, and figure out how to read arbitrary bytes.

view this post on Zulip Agus Zubiaga (Feb 28 2023 at 13:09):

If everything goes well I can probably make a PR later in the week!

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 21:33):

Got the API to a pretty good state I think: https://github.com/agu-z/roc-basic-cli/blob/sockets-support/src/Tcp.roc
Example: https://github.com/agu-z/roc-basic-cli/blob/sockets-support/examples/tcp-client.roc

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 21:35):

It can now read all data in the stream (was reading into a fixed buffer before). Also, you can read/write raw bytes if you want to.

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 21:38):

Replaced Tcp.connect with Tcp.withConnect which manages the lifetime of the connection so you can't forget to close, similar to: https://github.com/roc-lang/roc/blob/main/examples/cli/false-interpreter/platform/File.roc

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 21:40):

I think the only missing part for a PR is error handling. I wrote the types but glue doesn't work so I can't return them from Rust.

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 21:41):

I'll look into how much code per type we need, maybe I can just write them by hand

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 22:14):

Yeah, maybe not lol

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 22:16):

I'd rather spend the time investigating why glue doesn't work in the first place. Gotta run now but I might do that later or tomorrow.

view this post on Zulip Richard Feldman (Mar 01 2023 at 22:17):

I'd love to have someone else get involved with glue, so if you want to look into it that would be awesome! I'd be happy to help with answering any questions :smiley:

view this post on Zulip Agus Zubiaga (Mar 01 2023 at 22:22):

Yeah, it’ll probably take me a while to get familiar with the compiler internals but happy to!

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 18:54):

I managed to generate the glue code for my errors with this trick: https://roc.zulipchat.com/#narrow/stream/316715-contributing/topic/Broken.20glue/near/339383878

However, even if a I return a RocResult::ok from the rust, when I pattern match on the Roc side it always matches the Err case. Even if the platform is never creating RocResult::err at all.

view this post on Zulip Folkert de Vries (Mar 04 2023 at 18:55):

and neither of the a and x in Result a x are the [] type?

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 18:56):

Not in the Effect module or my Tcp module

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 18:56):

Only the main function because it needs to be Task _ []

view this post on Zulip Folkert de Vries (Mar 04 2023 at 18:56):

yeah so not in relevant code paths.

view this post on Zulip Folkert de Vries (Mar 04 2023 at 18:56):

ok, well this sounds like just a mismatch between where glue is storing and roc is reading the bytes

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 18:57):

Yeah, that's what I thought. Lemme try something like a RocStr from roc_std an error.

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 18:58):

Well, I guess that wouldn't matter because I'm never generating an error yet

view this post on Zulip Nick Hallstrom (Mar 04 2023 at 19:00):

Is that the same issue as this? Folkert’s response from the Roc Clock thread (can’t seem to copy the link from mobile):

well. this is interesting and frustrating: rust will send a RocResult<u8, ()> over to roc as a u16. we "receive" it as { [0 x i8], [1 x i8], i8, [0 x i8] }, which is reasonable (if you've seen LLVM IR before), but it is _not_ valid according to the C ABI

view this post on Zulip Nick Hallstrom (Mar 04 2023 at 19:01):

I was having the same issue where RocResult would always be Err

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 19:03):

I see. Yeah, I guess that'd make sense

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 19:20):

Ok yeah, I tried the other examples and it looks like Roc thinks all Result are Err in general

view this post on Zulip Brendan Hansknecht (Mar 04 2023 at 19:23):

Hmm. I wonder when that broke. It definitely has not always been the case.

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 19:23):

I mean all Result coming from rust to be clear

view this post on Zulip Folkert de Vries (Mar 04 2023 at 19:24):

I think it depends on the size of the type that is in a result

view this post on Zulip Folkert de Vries (Mar 04 2023 at 19:24):

I'd guess it works for i64/usize

view this post on Zulip Folkert de Vries (Mar 04 2023 at 19:24):

but not for most other things

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 19:28):

In my case I have a raw pointer in the Ok case and a glue generated enum in the Err case.

view this post on Zulip Agus Zubiaga (Mar 04 2023 at 19:32):

Should this be fixed in roc_std or in the compiler?

view this post on Zulip Folkert de Vries (Mar 04 2023 at 19:37):

this is a compiler thing


Last updated: Jul 05 2025 at 12:14 UTC