Skip to content

Commit b7b81a8

Browse files
authored
Merge pull request #265 from thomas-huegel/main
Upgrade to Mojo 25.6
2 parents fd22a8c + 1cb7d06 commit b7b81a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4379
-1931
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Once you have a Mojo project set up locally,
100100
from lightbug_http.strings import to_string
101101
from lightbug_http.header import HeaderKey
102102
103-
@value
103+
@fieldwise_init
104104
struct Printer(HTTPService):
105105
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
106106
print("Request URI:", req.uri.request_uri)
@@ -137,7 +137,7 @@ Routing is not in scope for this library, but you can easily set up routes yours
137137
```mojo
138138
from lightbug_http import *
139139
140-
@value
140+
@fieldwise_init
141141
struct ExampleRouter(HTTPService):
142142
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
143143
var body = req.body_raw
@@ -168,7 +168,7 @@ The default welcome screen shows an example of how to serve files like images or
168168
from lightbug_http import *
169169
from lightbug_http.io.bytes import Bytes
170170
171-
@value
171+
@fieldwise_init
172172
struct Welcome(HTTPService):
173173
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
174174
var uri = req.uri

benchmark/bench.mojo

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn run_benchmark():
2323
try:
2424
var config = BenchConfig()
2525
config.verbose_timing = True
26-
var m = Bench(config)
26+
var m = Bench(config^)
2727
m.bench_function[lightbug_benchmark_header_encode](BenchId("HeaderEncode"))
2828
m.bench_function[lightbug_benchmark_header_parse](BenchId("HeaderParse"))
2929
m.bench_function[lightbug_benchmark_request_encode](BenchId("RequestEncode"))
@@ -35,7 +35,7 @@ fn run_benchmark():
3535
print("failed to start benchmark")
3636

3737

38-
var headers_struct = Headers(
38+
alias headers_struct = Headers(
3939
Header("Content-Type", "application/json"),
4040
Header("Content-Length", "1234"),
4141
Header("Connection", "close"),
@@ -49,7 +49,7 @@ fn lightbug_benchmark_response_encode(mut b: Bencher):
4949
@always_inline
5050
@parameter
5151
fn response_encode():
52-
var res = HTTPResponse(body.as_bytes(), headers=headers_struct)
52+
var res = HTTPResponse(body.as_bytes(), headers=materialize[headers_struct]())
5353
_ = encode(res^)
5454

5555
b.iter[response_encode]()
@@ -89,8 +89,8 @@ fn lightbug_benchmark_request_encode(mut b: Bencher):
8989
var uri = URI.parse("http://127.0.0.1:8080/some-path")
9090
var req = HTTPRequest(
9191
uri=uri,
92-
headers=headers_struct,
93-
body=body_bytes,
92+
headers=materialize[headers_struct](),
93+
body=materialize[body_bytes](),
9494
)
9595
_ = encode(req^)
9696

@@ -106,7 +106,7 @@ fn lightbug_benchmark_header_encode(mut b: Bencher):
106106
@parameter
107107
fn header_encode():
108108
var b = ByteWriter()
109-
b.write(headers_struct)
109+
b.write(materialize[headers_struct]())
110110

111111
b.iter[header_encode]()
112112

lightbug_http/_libc.mojo

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from utils import StaticTuple
22
from sys.ffi import external_call, c_char, c_int, c_size_t, c_ssize_t, c_uchar, c_ushort, c_uint
3-
from sys.info import sizeof, os_is_windows, os_is_macos, os_is_linux
3+
from sys.info import size_of, CompilationTarget
44
from memory import memcpy, UnsafePointer, stack_allocation
55
from lightbug_http.io.bytes import Bytes
66

@@ -55,31 +55,31 @@ alias EPIPE = 32
5555
alias EDOM = 33
5656
alias ERANGE = 34
5757
alias EWOULDBLOCK = EAGAIN
58-
alias EINPROGRESS = 36 if os_is_macos() else 115
59-
alias EALREADY = 37 if os_is_macos() else 114
60-
alias ENOTSOCK = 38 if os_is_macos() else 88
61-
alias EDESTADDRREQ = 39 if os_is_macos() else 89
62-
alias EMSGSIZE = 40 if os_is_macos() else 90
63-
alias ENOPROTOOPT = 42 if os_is_macos() else 92
64-
alias EAFNOSUPPORT = 47 if os_is_macos() else 97
65-
alias EADDRINUSE = 48 if os_is_macos() else 98
66-
alias EADDRNOTAVAIL = 49 if os_is_macos() else 99
67-
alias ENETDOWN = 50 if os_is_macos() else 100
68-
alias ENETUNREACH = 51 if os_is_macos() else 101
69-
alias ECONNABORTED = 53 if os_is_macos() else 103
70-
alias ECONNRESET = 54 if os_is_macos() else 104
71-
alias ENOBUFS = 55 if os_is_macos() else 105
72-
alias EISCONN = 56 if os_is_macos() else 106
73-
alias ENOTCONN = 57 if os_is_macos() else 107
74-
alias ETIMEDOUT = 60 if os_is_macos() else 110
75-
alias ECONNREFUSED = 61 if os_is_macos() else 111
76-
alias ELOOP = 62 if os_is_macos() else 40
77-
alias ENAMETOOLONG = 63 if os_is_macos() else 36
78-
alias EHOSTUNREACH = 65 if os_is_macos() else 113
79-
alias EDQUOT = 69 if os_is_macos() else 122
80-
alias ENOMSG = 91 if os_is_macos() else 42
81-
alias EPROTO = 100 if os_is_macos() else 71
82-
alias EOPNOTSUPP = 102 if os_is_macos() else 95
58+
alias EINPROGRESS = 36 if CompilationTarget.is_macos() else 115
59+
alias EALREADY = 37 if CompilationTarget.is_macos() else 114
60+
alias ENOTSOCK = 38 if CompilationTarget.is_macos() else 88
61+
alias EDESTADDRREQ = 39 if CompilationTarget.is_macos() else 89
62+
alias EMSGSIZE = 40 if CompilationTarget.is_macos() else 90
63+
alias ENOPROTOOPT = 42 if CompilationTarget.is_macos() else 92
64+
alias EAFNOSUPPORT = 47 if CompilationTarget.is_macos() else 97
65+
alias EADDRINUSE = 48 if CompilationTarget.is_macos() else 98
66+
alias EADDRNOTAVAIL = 49 if CompilationTarget.is_macos() else 99
67+
alias ENETDOWN = 50 if CompilationTarget.is_macos() else 100
68+
alias ENETUNREACH = 51 if CompilationTarget.is_macos() else 101
69+
alias ECONNABORTED = 53 if CompilationTarget.is_macos() else 103
70+
alias ECONNRESET = 54 if CompilationTarget.is_macos() else 104
71+
alias ENOBUFS = 55 if CompilationTarget.is_macos() else 105
72+
alias EISCONN = 56 if CompilationTarget.is_macos() else 106
73+
alias ENOTCONN = 57 if CompilationTarget.is_macos() else 107
74+
alias ETIMEDOUT = 60 if CompilationTarget.is_macos() else 110
75+
alias ECONNREFUSED = 61 if CompilationTarget.is_macos() else 111
76+
alias ELOOP = 62 if CompilationTarget.is_macos() else 40
77+
alias ENAMETOOLONG = 63 if CompilationTarget.is_macos() else 36
78+
alias EHOSTUNREACH = 65 if CompilationTarget.is_macos() else 113
79+
alias EDQUOT = 69 if CompilationTarget.is_macos() else 122
80+
alias ENOMSG = 91 if CompilationTarget.is_macos() else 42
81+
alias EPROTO = 100 if CompilationTarget.is_macos() else 71
82+
alias EOPNOTSUPP = 102 if CompilationTarget.is_macos() else 95
8383

8484
# --- ( Network Related Constants )---------------------------------------------
8585
alias sa_family_t = c_ushort
@@ -91,7 +91,7 @@ alias in_port_t = c_ushort
9191
# TODO: These might vary on each platform...we should confirm this.
9292
# Taken from: https://github.com/openbsd/src/blob/master/sys/sys/socket.h#L250
9393
# Address Family Constants
94-
@value
94+
@fieldwise_init
9595
@register_passable("trivial")
9696
struct AddressFamily(EqualityComparable & Copyable & Movable):
9797
var value: Int32
@@ -197,7 +197,7 @@ struct AddressFamily(EqualityComparable & Copyable & Movable):
197197
return self.value != other.value
198198

199199

200-
@value
200+
@fieldwise_init
201201
@register_passable("trivial")
202202
struct AddressLength:
203203
var value: Int
@@ -329,19 +329,18 @@ alias SO_PROTOCOL = 0x1025
329329

330330

331331
# --- ( Network Related Structs )-----------------------------------------------
332-
@value
332+
@fieldwise_init
333333
@register_passable("trivial")
334334
struct in_addr:
335335
var s_addr: in_addr_t
336336

337337

338-
@value
338+
@fieldwise_init
339339
@register_passable("trivial")
340340
struct in6_addr:
341341
var s6_addr: StaticTuple[c_char, 16]
342342

343343

344-
@value
345344
@register_passable("trivial")
346345
struct sockaddr:
347346
var sa_family: sa_family_t
@@ -352,7 +351,7 @@ struct sockaddr:
352351
self.sa_data = data
353352

354353

355-
@value
354+
@fieldwise_init
356355
@register_passable("trivial")
357356
struct sockaddr_in:
358357
var sin_family: sa_family_t
@@ -374,7 +373,7 @@ struct sockaddr_in:
374373
self.sin_zero = StaticTuple[c_char, 8](0, 0, 0, 0, 0, 0, 0, 0)
375374

376375

377-
@value
376+
@fieldwise_init
378377
@register_passable("trivial")
379378
struct sockaddr_in6:
380379
var sin6_family: sa_family_t
@@ -384,7 +383,7 @@ struct sockaddr_in6:
384383
var sin6_scope_id: c_uint
385384

386385

387-
@value
386+
@fieldwise_init
388387
@register_passable("trivial")
389388
struct addrinfo:
390389
var ai_flags: c_int
@@ -620,7 +619,7 @@ fn _inet_pton(af: c_int, src: UnsafePointer[c_char, mut=False], dst: UnsafePoint
620619
](af, src, dst)
621620

622621

623-
fn inet_pton[address_family: AddressFamily](owned src: String) raises -> c_uint:
622+
fn inet_pton[address_family: AddressFamily](var src: String) raises -> c_uint:
624623
"""Libc POSIX `inet_pton` function. Converts a presentation format address (that is, printable form as held in a character string)
625624
to network format (usually a struct in_addr or some other internal binary representation, in network byte order).
626625
@@ -657,7 +656,7 @@ fn inet_pton[address_family: AddressFamily](owned src: String) raises -> c_uint:
657656
else:
658657
ip_buffer = stack_allocation[4, c_void]()
659658

660-
var result = _inet_pton(address_family.value, src.unsafe_cstr_ptr().origin_cast[mut=False](), ip_buffer)
659+
var result = _inet_pton(address_family.value, src.unsafe_cstr_ptr().origin_cast[False](), ip_buffer)
661660
if result == 0:
662661
raise Error("inet_pton Error: The input is not a valid address.")
663662
elif result == -1:
@@ -825,7 +824,7 @@ fn setsockopt(
825824
#### Notes:
826825
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html .
827826
"""
828-
var result = _setsockopt(socket, level, option_name, Pointer(to=option_value), sizeof[Int]())
827+
var result = _setsockopt(socket, level, option_name, Pointer(to=option_value), size_of[Int]())
829828
if result == -1:
830829
var errno = get_errno()
831830
if errno == EBADF:
@@ -916,7 +915,7 @@ fn getsockopt(
916915
* Reference: https://man7.org/linux/man-pages/man3/getsockopt.3p.html .
917916
"""
918917
var option_value = stack_allocation[1, c_void]()
919-
var option_len: socklen_t = sizeof[Int]()
918+
var option_len: socklen_t = size_of[Int]()
920919
var result = _getsockopt(socket, level, option_name, option_value, Pointer(to=option_len))
921920
if result == -1:
922921
var errno = get_errno()
@@ -1067,7 +1066,7 @@ fn getpeername(file_descriptor: c_int) raises -> sockaddr_in:
10671066
* Reference: https://man7.org/linux/man-pages/man2/getpeername.2.html .
10681067
"""
10691068
var remote_address = stack_allocation[1, sockaddr]()
1070-
var result = _getpeername(file_descriptor, remote_address, Pointer(to=socklen_t(sizeof[sockaddr]())))
1069+
var result = _getpeername(file_descriptor, remote_address, Pointer(to=socklen_t(size_of[sockaddr]())))
10711070
if result == -1:
10721071
var errno = get_errno()
10731072
if errno == EBADF:
@@ -1151,7 +1150,7 @@ fn bind(socket: c_int, address: sockaddr_in) raises:
11511150
#### Notes:
11521151
* Reference: https://man7.org/linux/man-pages/man3/bind.3p.html .
11531152
"""
1154-
var result = _bind(socket, Pointer(to=address), sizeof[sockaddr_in]())
1153+
var result = _bind(socket, Pointer(to=address), size_of[sockaddr_in]())
11551154
if result == -1:
11561155
var errno = get_errno()
11571156
if errno == EACCES:
@@ -1308,7 +1307,7 @@ fn accept(socket: c_int) raises -> c_int:
13081307
* Reference: https://man7.org/linux/man-pages/man3/accept.3p.html .
13091308
"""
13101309
var remote_address = sockaddr()
1311-
var result = _accept(socket, Pointer(to=remote_address), Pointer(to=socklen_t(sizeof[socklen_t]())))
1310+
var result = _accept(socket, Pointer(to=remote_address), Pointer(to=socklen_t(size_of[socklen_t]())))
13121311
if result == -1:
13131312
var errno = get_errno()
13141313
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
@@ -1349,7 +1348,7 @@ fn accept(socket: c_int) raises -> c_int:
13491348
raise Error("accept: Protocol error.")
13501349

13511350
@parameter
1352-
if os_is_linux():
1351+
if CompilationTarget.is_linux():
13531352
if errno == EPERM:
13541353
raise Error("accept: Firewall rules forbid connection.")
13551354
raise Error("accept: An error occurred while listening on the socket. Error code: " + String(errno))
@@ -1410,7 +1409,7 @@ fn connect(socket: c_int, address: sockaddr_in) raises:
14101409
#### Notes:
14111410
* Reference: https://man7.org/linux/man-pages/man3/connect.3p.html .
14121411
"""
1413-
var result = _connect(socket, Pointer(to=address), sizeof[sockaddr_in]())
1412+
var result = _connect(socket, Pointer(to=address), size_of[sockaddr_in]())
14141413
if result == -1:
14151414
var errno = get_errno()
14161415
if errno == EACCES:
@@ -1637,7 +1636,7 @@ fn recvfrom(
16371636
* `MSG_WAITALL`: On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function may return the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.
16381637
16391638
"""
1640-
var result = _recvfrom(socket, buffer, length, flags, address, Pointer[socklen_t](to=sizeof[sockaddr]()))
1639+
var result = _recvfrom(socket, buffer, length, flags, address, Pointer[socklen_t](to=size_of[sockaddr]()))
16411640
if result == -1:
16421641
var errno = get_errno()
16431642
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
@@ -1898,7 +1897,7 @@ fn sendto(
18981897
* `MSG_NOSIGNAL`: Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. The [EPIPE] error shall still be returned.
18991898
19001899
"""
1901-
var result = _sendto(socket, message, length, flags, dest_addr, sizeof[sockaddr]())
1900+
var result = _sendto(socket, message, length, flags, dest_addr, size_of[sockaddr]())
19021901
if result == -1:
19031902
var errno = get_errno()
19041903
if errno == EAFNOSUPPORT:
@@ -2118,10 +2117,10 @@ fn get_errno() -> c_int:
21182117
"""
21192118

21202119
@parameter
2121-
if os_is_windows():
2120+
if CompilationTarget.is_windows():
21222121
var errno = stack_allocation[1, c_int]()
21232122
_ = external_call["_get_errno", c_void](errno)
21242123
return errno[]
21252124
else:
2126-
alias loc = "__error" if os_is_macos() else "__errno_location"
2125+
alias loc = "__error" if CompilationTarget.is_macos() else "__errno_location"
21272126
return external_call[loc, UnsafePointer[c_int]]()[]

lightbug_http/_logger.mojo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ mojo ... -D LB_LOG_LEVEL=DEBUG
4646
"""
4747

4848

49-
@value
50-
struct Logger[level: Int]:
49+
@fieldwise_init
50+
struct Logger[level: Int](Movable, ImplicitlyCopyable):
5151
fn _log_message[event_level: Int](self, message: String):
5252
@parameter
5353
if level >= event_level:

0 commit comments

Comments
 (0)