1// Copyright 2025 International Digital Economy Academy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///|
16type SockaddrIn 
Bytes
Bytes
17 18///| 19extern "c" fn uv_sockaddr_in_make() ->
type SockaddrIn Bytes
SockaddrIn
= "moonbit_uv_sockaddr_in_make"
20 21///| 22type SockaddrIn6
Bytes
Bytes
23 24///| 25extern "c" fn uv_sockaddr_in6_make() ->
type SockaddrIn6 Bytes
SockaddrIn6
= "moonbit_uv_sockaddr_in6_make"
26 27///| 28type Sockaddr
Bytes
Bytes
29 30///| 31pub trait
trait ToSockaddr {
  to_sockaddr(self : Self) -> Sockaddr
  ip_name(self : Self) -> String raise Errno
}
ToSockaddr
{
32
(Self) -> Sockaddr
to_sockaddr
(self :

type parameter Self

Self
) ->
type Sockaddr Bytes
Sockaddr
33
(Self) -> String raise Errno
ip_name
(self :

type parameter Self

Self
) ->
String
String
raise
type! Errno Status
Errno
34} 35 36///| 37pub impl
trait ToSockaddr {
  to_sockaddr(self : Self) -> Sockaddr
  ip_name(self : Self) -> String raise Errno
}
ToSockaddr
for
type Sockaddr Bytes
Sockaddr
with to_sockaddr(self :
type Sockaddr Bytes
Sockaddr
) ->
type Sockaddr Bytes
Sockaddr
= "%identity"
38 39///| 40pub impl
trait ToSockaddr {
  to_sockaddr(self : Self) -> Sockaddr
  ip_name(self : Self) -> String raise Errno
}
ToSockaddr
for
type SockaddrIn Bytes
SockaddrIn
with to_sockaddr(addr :
type SockaddrIn Bytes
SockaddrIn
) ->
type Sockaddr Bytes
Sockaddr
= "%identity"
41 42///| 43pub impl
trait ToSockaddr {
  to_sockaddr(self : Self) -> Sockaddr
  ip_name(self : Self) -> String raise Errno
}
ToSockaddr
for
type SockaddrIn6 Bytes
SockaddrIn6
with to_sockaddr(addr :
type SockaddrIn6 Bytes
SockaddrIn6
) ->
type Sockaddr Bytes
Sockaddr
= "%identity"
44 45///| 46enum SockType { 47
SockType
Stream
48
SockType
Raw
49
SockType
Datagram
50} derive(
trait Show {
  output(Self, &Logger) -> Unit
  to_string(Self) -> String
}

Trait for types that can be converted to String

Show
)
51 52///| 53pub fn
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
::
() -> SockType
stream
() ->
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
{
54 return
SockType
Stream
55} 56 57///| 58pub fn
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
::
() -> SockType
raw
() ->
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
{
59 return
SockType
Raw
60} 61 62///| 63#deprecated("Use SockType::datagram() instead") 64pub fn
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
::
() -> SockType
dgram
() ->
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
{
65 return
SockType
Datagram
66} 67 68///| 69pub fn
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
::
() -> SockType
datagram
() ->
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
{
70 return
SockType
Datagram
71} 72 73///| 74extern "c" fn uv_SOCK_STREAM() ->
Int
Int
= "moonbit_uv_SOCK_STREAM"
75 76///| 77let
Int
_SOCK_STREAM
:
Int
Int
=
() -> Int
uv_SOCK_STREAM
()
78 79///| 80extern "c" fn uv_SOCK_DGRAM() ->
Int
Int
= "moonbit_uv_SOCK_DGRAM"
81 82///| 83let
Int
_SOCK_DGRAM
:
Int
Int
=
() -> Int
uv_SOCK_DGRAM
()
84 85///| 86extern "c" fn uv_SOCK_RAW() ->
Int
Int
= "moonbit_uv_SOCK_RAW"
87 88///| 89let
Int
_SOCK_RAW
:
Int
Int
=
() -> Int
uv_SOCK_RAW
()
90 91///| 92fn
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
::
(self : Int) -> SockType raise Errno
of_int
(
Int
self
:
Int
Int
) ->
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
raise
type! Errno Status
Errno
{
93 if
Int
self
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
Int
_SOCK_STREAM
{
94 return
SockType
Stream
95 } 96 if
Int
self
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
Int
_SOCK_RAW
{
97 return
SockType
Raw
98 } 99 if
Int
self
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
Int
_SOCK_DGRAM
{
100 return
SockType
Datagram
101 } 102 raise
(Status) -> Errno
Errno
(
Status
EINVAL
)
103} 104 105///| 106fn
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
::
(self : SockType) -> Int
to_int
(
SockType
self
:
enum SockType {
  Stream
  Raw
  Datagram
}
SockType
) ->
Int
Int
{
107 match
SockType
self
{
108
SockType
Stream
=>
Int
_SOCK_STREAM
109
SockType
Raw
=>
Int
_SOCK_RAW
110
SockType
Datagram
=>
Int
_SOCK_DGRAM
111 } 112} 113