Run all lua examples in docs as doctests

This commit is contained in:
2025-06-30 22:01:00 +10:00
parent 81cd901ea6
commit da2598b534
3 changed files with 65 additions and 29 deletions

View File

@@ -224,9 +224,9 @@ impl lb_netlib {
///
/// ```lua
/// local net = require("lb:net")
/// local socket = net.bind_tcp("127.0.0.1", 8080)
/// local socket = net.bind_tcp("127.0.0.1")
///
/// assert(socket:local_addr() == net.socketaddr("127.0.0.1:8080"))
/// assert(socket:local_addr():ip() == net.ipaddr("127.0.0.1"))
/// socket:set_nodelay(true)
/// ```
pub extern "Lua" fn bind_tcp(
@@ -257,15 +257,16 @@ impl lb_netlib {
///
/// # Example
///
/// ```lua
/// ```lua,no_run
/// local net = require("lb:net")
/// local listener = net.listen_tcp("127.0.0.1", 1234)
/// local listener = net.listen_tcp("127.0.0.1")
///
/// assert(listener:local_addr() == net.socketaddr("127.0.0.1:1234"))
/// assert(listener:local_addr():ip() == net.ipaddr("127.0.0.1"))
///
/// for stream in listener do
/// print("client connected: ", stream:remote_addr())
/// print("client connected: ", stream:peer_addr())
/// end
/// ```
pub extern "Lua" fn listen_tcp(
addr: OneOf<(&str, &lb_ipaddr, &lb_socketaddr)>,
port: Option<u16>,
@@ -287,9 +288,10 @@ impl lb_netlib {
///
/// ```lua
/// local net = require("lb:net")
/// local stream = net.connect_tcp("127.0.0.1", 1234)
/// local listener = net.listen_tcp("127.0.0.1")
/// local stream = net.connect_tcp("127.0.0.1", listener:local_addr():port())
///
/// assert(stream:remote_addr() == net.socketaddr("127.0.0.1:1234"))
/// assert(stream:peer_addr():ip() == net.ipaddr("127.0.0.1"))
/// stream:write("Hello, server!\n")
/// ```
pub async extern "Lua" fn connect_tcp(

View File

@@ -183,7 +183,7 @@ impl lb_tcpsocket {
///
/// This examples spawns a reader task and a writer task to operate on the stream concurrently.
///
/// ```lua
/// ```lua,no_run
/// local task = require("lb:task")
/// local net = require("lb:net")
/// local socket = net.connect_tcp("127.0.0.1:1234")
@@ -194,17 +194,17 @@ impl lb_tcpsocket {
/// local reader = spawn(function()
/// for chunk in socket, 1024 do
/// print("received: ", chunk)
/// done
/// end
///
/// print("done reading")
/// end)
///
/// local writer = spawn(function()
/// for i = 1, 10 do
/// local msg = ("message %d\n"):format(i)
/// local msg = ("message %d"):format(i)
/// socket:write(msg)
/// print("sent: ", msg)
/// done
/// end
///
/// print("done writing")
/// end)
@@ -215,7 +215,10 @@ impl lb_tcpsocket {
/// The above example uses the socket as an iterator in a generic `for` loop to read data in chunks
/// of up to 1024 bytes. It is equivalent to the following:
///
/// ```lua
/// ```lua,no_run
/// local net = require("lb:net")
/// local socket = net.connect_tcp("127.0.0.1:1234")
///
/// while true do
/// local chunk = socket:read_partial(1024)
/// if chunk == nil then break end
@@ -430,7 +433,7 @@ impl lb_tcpstream {
///
/// The listener can be used as an iterator in a generic `for` loop to accept incoming connections:
///
/// ```lua
/// ```lua,no_run
/// local net = require("lb:net")
/// local listener = net.listen_tcp("127.0.0.1")
///