Skip to content

Commit b0b57ab

Browse files
committed
Add convenience methods to Storable object
The methods call the corresponding type class method for the type parameter
1 parent 159134b commit b0b57ab

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/llvm-ffi/scala/ffi/Ptr.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Ptr[T] private[ffi] (private[ffi] val address: CIntPtr) {
3030
def poke(x: T)(implicit st: Storable[T]): Unit = st.poke(this, x)
3131
def peekElemOff(n: Int)(implicit st: Storable[T]): T = st.peekElemOff(this, n)
3232
def pokeElemOff(n: Int, x: T)(implicit st: Storable[T]): Unit = st.pokeElemOff(this, n, x)
33-
def peekByteOff[S:Storable](n: Int) = implicitly[Storable[S]].peekByteOff(this, n)
34-
def pokeByteOff[S:Storable](n: Int, x: S) = implicitly[Storable[S]].pokeByteOff(this, n, x)
33+
def peekByteOff[S:Storable](n: Int): S = Storable.peekByteOff(this, n)
34+
def pokeByteOff[S:Storable](n: Int, x: S): Unit = Storable.pokeByteOff(this, n, x)
3535
def isNull = address == 0
3636
}

src/llvm-ffi/scala/ffi/Storable.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ package scala.ffi
33
object Storable extends ctypes.StorableCtypes
44
{
55
def get[T:Storable] = implicitly[Storable[T]]
6+
def size[T:Storable] = get[T].size
7+
def alignment[T:Storable] = get[T].alignment
8+
def peekElemOff[T:Storable](base: Ptr[T], n: Int): T = get[T].peekElemOff(base, n)
9+
def pokeElemOff[T:Storable](base: Ptr[T], n: Int, x: T): Unit = get[T].pokeElemOff(base, n, x)
10+
def peekByteOff[T:Storable](base: Ptr[_], n: Int): T = get[T].peekByteOff(base, n)
11+
def pokeByteOff[T:Storable](base: Ptr[_], n: Int, x: T): Unit = get[T].pokeByteOff(base, n, x)
12+
def peek[T:Storable](ptr: Ptr[T]): T = get[T].peek(ptr)
13+
def poke[T:Storable](ptr: Ptr[T], x: T): Unit = get[T].poke(ptr, x)
614
}
715

816
trait Storable[T] {

0 commit comments

Comments
 (0)