@@ -59,7 +59,7 @@ object Memory:
59
59
* @return
60
60
* The result of applying f to the new memory segment
61
61
*/
62
- def initWith [A : Layout as l ](size : Int )[B , S ](f : Memory [A ] => B < S )(using frame : Frame ): B < (Arena & S ) =
62
+ def initWith [A : Layout as l ](size : Int )[B , S ](f : Memory [A ] => B < S )(using Frame ): B < (Arena & S ) =
63
63
Arena .use { arena =>
64
64
IO .Unsafe (f(arena.allocate(l.size * size)))
65
65
}
@@ -73,7 +73,7 @@ object Memory:
73
73
* @return
74
74
* The element at the specified index
75
75
*/
76
- def get (index : Int )(using Frame ): A < Arena =
76
+ inline def get (index : Int )(using inline frame : Frame ): A < Arena =
77
77
IO .Unsafe {
78
78
// TODO inference issue without the val
79
79
val x = Unsafe .get(self)(index)
@@ -88,15 +88,15 @@ object Memory:
88
88
* @param value
89
89
* The value to write
90
90
*/
91
- def set (index : Int , value : A )(using Frame ): Unit < Arena =
91
+ inline def set (index : Int , value : A )(using inline frame : Frame ): Unit < Arena =
92
92
IO .Unsafe (Unsafe .set(self)(index, value))
93
93
94
94
/** Fills the entire memory segment with the specified value.
95
95
*
96
96
* @param value
97
97
* The value to fill with
98
98
*/
99
- def fill (value : A )(using frame : Frame ): Unit < Arena =
99
+ inline def fill (value : A )(using inline frame : Frame ): Unit < Arena =
100
100
IO .Unsafe (Unsafe .fill(self)(value))
101
101
102
102
/** Folds over all elements in the memory segment.
@@ -108,7 +108,7 @@ object Memory:
108
108
* @return
109
109
* The final accumulated value
110
110
*/
111
- def fold [B ](zero : B )(f : (B , A ) => B )(using Frame ): B < Arena =
111
+ inline def fold [B ](zero : B )(f : (B , A ) => B )(using inline frame : Frame ): B < Arena =
112
112
IO .Unsafe (Unsafe .fold(self)(zero)(f))
113
113
114
114
/** Finds the index of the first element satisfying the predicate.
@@ -118,7 +118,7 @@ object Memory:
118
118
* @return
119
119
* The index wrapped in Maybe, or Absent if not found
120
120
*/
121
- def findIndex (p : A => Boolean )(using Frame ): Maybe [Int ] < Arena =
121
+ inline def findIndex (p : A => Boolean )(using inline frame : Frame ): Maybe [Int ] < Arena =
122
122
IO .Unsafe (Unsafe .findIndex(self)(p))
123
123
124
124
/** Checks if any element satisfies the predicate.
@@ -128,7 +128,7 @@ object Memory:
128
128
* @return
129
129
* true if any element satisfies the predicate
130
130
*/
131
- def exists (p : A => Boolean )(using Frame ): Boolean < Arena =
131
+ inline def exists (p : A => Boolean )(using inline frame : Frame ): Boolean < Arena =
132
132
IO .Unsafe (Unsafe .exists(self)(p))
133
133
134
134
/** Creates a view of a portion of this memory segment.
@@ -208,13 +208,13 @@ object Memory:
208
208
/** WARNING: Low-level API meant for integrations, libraries, and performance-sensitive code. See AllowUnsafe for more details. */
209
209
object Unsafe :
210
210
extension [A : Layout as l ](self : Unsafe [A ])
211
- def get (index : Int )(using AllowUnsafe ): A =
211
+ inline def get (index : Int )(using AllowUnsafe ): A =
212
212
l.get(self, index * l.size)
213
213
214
- def set (index : Int , value : A )(using AllowUnsafe ): Unit =
214
+ inline def set (index : Int , value : A )(using AllowUnsafe ): Unit =
215
215
l.set(self, index * l.size, value)
216
216
217
- def fill (value : A )(using AllowUnsafe ): Unit =
217
+ inline def fill (value : A )(using AllowUnsafe ): Unit =
218
218
val len = size
219
219
@ tailrec def loop (i : Int ): Unit =
220
220
if i < len then
@@ -223,7 +223,7 @@ object Memory:
223
223
loop(0 )
224
224
end fill
225
225
226
- def fold [B ](z : B )(f : (B , A ) => B )(using AllowUnsafe ): B =
226
+ inline def fold [B ](z : B )(inline f : (B , A ) => B )(using AllowUnsafe ): B =
227
227
val len = size
228
228
@ tailrec def loop (i : Int , acc : B ): B =
229
229
if i < len then
@@ -232,7 +232,7 @@ object Memory:
232
232
loop(0 , z)
233
233
end fold
234
234
235
- def findIndex (f : A => Boolean )(using AllowUnsafe ): Maybe [Int ] =
235
+ inline def findIndex (inline f : A => Boolean )(using AllowUnsafe ): Maybe [Int ] =
236
236
val len = size
237
237
@ tailrec def loop (i : Int ): Maybe [Int ] =
238
238
if i < len then
@@ -242,7 +242,7 @@ object Memory:
242
242
loop(0 )
243
243
end findIndex
244
244
245
- def exists (f : A => Boolean )(using AllowUnsafe ): Boolean =
245
+ inline def exists (inline f : A => Boolean )(using AllowUnsafe ): Boolean =
246
246
findIndex(f) match
247
247
case Present (_) => true
248
248
case Absent => false
@@ -268,8 +268,8 @@ object Memory:
268
268
269
269
/** Defines how values of type A are laid out in memory. */
270
270
abstract class Layout [A ]:
271
- def get (memory : Unsafe [A ], offset : Long )(using AllowUnsafe ): A
272
- def set (memory : Unsafe [A ], offset : Long , value : A )(using AllowUnsafe ): Unit
271
+ inline def get (memory : Unsafe [A ], offset : Long )(using AllowUnsafe ): A
272
+ inline def set (memory : Unsafe [A ], offset : Long , value : A )(using AllowUnsafe ): Unit
273
273
def size : Long
274
274
end Layout
275
275
@@ -278,49 +278,49 @@ object Memory:
278
278
import ValueLayout .*
279
279
280
280
given Layout [Byte ] with
281
- def get (memory : Unsafe [Byte ], offset : Long )(using AllowUnsafe ): Byte =
281
+ inline def get (memory : Unsafe [Byte ], offset : Long )(using AllowUnsafe ): Byte =
282
282
memory.get(JAVA_BYTE , offset)
283
- def set (memory : Unsafe [Byte ], offset : Long , value : Byte )(using AllowUnsafe ): Unit =
283
+ inline def set (memory : Unsafe [Byte ], offset : Long , value : Byte )(using AllowUnsafe ): Unit =
284
284
memory.set(JAVA_BYTE , offset, value)
285
285
def size = JAVA_BYTE .byteSize
286
286
end given
287
287
288
288
given Layout [Short ] with
289
- def get (memory : Unsafe [Short ], offset : Long )(using AllowUnsafe ): Short =
289
+ inline def get (memory : Unsafe [Short ], offset : Long )(using AllowUnsafe ): Short =
290
290
memory.get(JAVA_SHORT , offset)
291
- def set (memory : Unsafe [Short ], offset : Long , value : Short )(using AllowUnsafe ): Unit =
291
+ inline def set (memory : Unsafe [Short ], offset : Long , value : Short )(using AllowUnsafe ): Unit =
292
292
memory.set(JAVA_SHORT , offset, value)
293
293
def size = JAVA_SHORT .byteSize
294
294
end given
295
295
296
296
given Layout [Int ] with
297
- def get (memory : Unsafe [Int ], offset : Long )(using AllowUnsafe ): Int =
297
+ inline def get (memory : Unsafe [Int ], offset : Long )(using AllowUnsafe ): Int =
298
298
memory.get(JAVA_INT , offset)
299
- def set (memory : Unsafe [Int ], offset : Long , value : Int )(using AllowUnsafe ): Unit =
299
+ inline def set (memory : Unsafe [Int ], offset : Long , value : Int )(using AllowUnsafe ): Unit =
300
300
memory.set(JAVA_INT , offset, value)
301
301
def size = JAVA_INT .byteSize
302
302
end given
303
303
304
304
given Layout [Long ] with
305
- def get (memory : Unsafe [Long ], offset : Long )(using AllowUnsafe ): Long =
305
+ inline def get (memory : Unsafe [Long ], offset : Long )(using AllowUnsafe ): Long =
306
306
memory.get(JAVA_LONG , offset)
307
- def set (memory : Unsafe [Long ], offset : Long , value : Long )(using AllowUnsafe ): Unit =
307
+ inline def set (memory : Unsafe [Long ], offset : Long , value : Long )(using AllowUnsafe ): Unit =
308
308
memory.set(JAVA_LONG , offset, value)
309
309
def size = JAVA_LONG .byteSize
310
310
end given
311
311
312
312
given Layout [Float ] with
313
- def get (memory : Unsafe [Float ], offset : Long )(using AllowUnsafe ): Float =
313
+ inline def get (memory : Unsafe [Float ], offset : Long )(using AllowUnsafe ): Float =
314
314
memory.get(JAVA_FLOAT , offset)
315
- def set (memory : Unsafe [Float ], offset : Long , value : Float )(using AllowUnsafe ): Unit =
315
+ inline def set (memory : Unsafe [Float ], offset : Long , value : Float )(using AllowUnsafe ): Unit =
316
316
memory.set(JAVA_FLOAT , offset, value)
317
317
def size = JAVA_FLOAT .byteSize
318
318
end given
319
319
320
320
given Layout [Double ] with
321
- def get (memory : Unsafe [Double ], offset : Long )(using AllowUnsafe ): Double =
321
+ inline def get (memory : Unsafe [Double ], offset : Long )(using AllowUnsafe ): Double =
322
322
memory.get(JAVA_DOUBLE , offset)
323
- def set (memory : Unsafe [Double ], offset : Long , value : Double )(using AllowUnsafe ): Unit =
323
+ inline def set (memory : Unsafe [Double ], offset : Long , value : Double )(using AllowUnsafe ): Unit =
324
324
memory.set(JAVA_DOUBLE , offset, value)
325
325
def size = JAVA_DOUBLE .byteSize
326
326
end given
0 commit comments