/
nv-lang
/
nova
Обзор
Документация
Войти
/
nv-lang
/
nova
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
std/src/net/effect.nv
68 строк
4 KB
Evgeniy Golovin
fix(net): порт унифицирован до int на всей публичной поверхности
09 авг 2026, 15:04
09 авг 2026, 15:04
6369339
Код
Авторство
О чём код?
// SPDX-License-Identifier: MIT OR Apache-2.0 // std/net/effect.nv — the single unified `Net` effect. // // One mockable dispatch point for all transport: TCP + UDP + DNS. Concrete // handlers: real_net() (tcp.nv) / mock_net() (mock.nv). // // BYTE SURFACE: every transport operation carries `[]u8`, never // `str`. Reads follow the Go/Rust `read(buf) -> n` zero-copy shape — the caller // owns the buffer, the op fills it and returns the count. This also sidesteps // the effect-vtable Ok-payload erasure (a `Result[[]u8, _]` return erases to // nova_int; a buffer-fill returning `int` does not — same rule as std/io). str // conveniences (`read_text`, …) are user-side `.nv` helpers over these bytes via // `str.from_bytes -> Result[str, Utf8Error]`, NOT effect operations (tcp.nv). // // Addresses never cross the vtable as a multi-word RETURN either: address getters // and the UDP sender fill a caller 20-byte []u8 image, which the public method // wraps into a `SocketAddr` value. `SocketAddr` DOES appear as an ARGUMENT // (multi-word args are fine — cf. std/io `read_in(buf mut []u8)`). // // Operations that wait on I/O (accept/connect/read/write/send_to/recv_from/ // lookup) park the calling fiber inside real_net and resume from the libuv loop; // the caller sees a synchronous-looking call carrying only the `Net` effect. module std.net /// Unified TCP + UDP + DNS transport. All fallible operations return /// `Result[T, NetError]`. #stable(since = "0.1") export type Net effect { // ── TCP ────────────────────────────────────────────────────────────────── tcp_bind(addr SocketAddr) -> Result[TcpListener, NetError] accept(listener TcpListener) -> Result[TcpStream, NetError] connect(addr SocketAddr) -> Result[TcpStream, NetError] // Fill buf[0..n] from the stream; Ok(0) = clean EOF (peer close). read(stream TcpStream, buf mut []u8) -> Result[int, NetError] // Send data; Ok(n) = bytes accepted. write(stream TcpStream, data []u8) -> Result[int, NetError] shutdown(stream TcpStream) -> Result[(), NetError] close_stream(stream TcpStream) -> () close_listener(listener TcpListener) -> () mark_split(stream TcpStream) -> () listener_local_port(listener TcpListener) -> int listener_local_addr(listener TcpListener, out mut []u8) -> () stream_local_port(stream TcpStream) -> int stream_peer_port(stream TcpStream) -> int stream_local_addr(stream TcpStream, out mut []u8) -> () stream_peer_addr(stream TcpStream, out mut []u8) -> () set_nodelay(stream TcpStream, on bool) -> () set_keepalive(stream TcpStream, on bool) -> () set_reuse_address(listener TcpListener, on bool) -> () // ── UDP ────────────────────────────────────────────────────────────────── udp_bind(addr SocketAddr) -> Result[UdpSocket, NetError] send_to(sock UdpSocket, data []u8, addr SocketAddr) -> Result[int, NetError] // Datagram into buf[0..n]; sender's 20-byte image into `sender`. recv_from(sock UdpSocket, buf mut []u8, sender mut []u8) -> Result[int, NetError] close_socket(sock UdpSocket) -> () socket_local_port(sock UdpSocket) -> int socket_local_addr(sock UdpSocket, out mut []u8) -> () // ── DNS ────────────────────────────────────────────────────────────────── // Resolve (host):port in one call. Returns (array_base, count) where // `array_base` is the GC address-image array pointer as an int; `resolve` // (dns.nv) builds the owned []SocketAddr from it OUTSIDE the vtable (a // Vec-of-value-records Ok payload does not survive the effect vtable's GC // tracing — the scalar pair does; the array stays live via `array_base`). lookup(host []u8, port int) -> Result[(int, int), NetError] }