0 | module System.UV.Pointer
 1 |
 2 | import IO.Async
 3 | import Data.Buffer
 4 | import Data.Buffer.Indexed
 5 | import Data.ByteString
 6 | import System.FFI
 7 | import System.UV.Util
 8 |
 9 | import public System.UV.Raw.Pointer
10 |
11 | %default total
12 |
13 | export %inline
14 | Resource (Ptr Bits8) where
15 |   release = freePtr
16 |
17 | export %inline
18 | Resource (Ptr Char) where
19 |   release = freePtr
20 |
21 | --------------------------------------------------------------------------------
22 | -- String Conversions
23 | --------------------------------------------------------------------------------
24 |
25 | ||| Reads `n` bytes of data from the byte array in a `uv_buf_t`
26 | ||| into an Idris-managed immutable `ByteString`
27 | export
28 | toByteString : HasIO io => Ptr Bits8 -> Bits32 -> io ByteString
29 | toByteString p x = do
30 |   buf <- newBuffer x
31 |   copyToBuffer p buf x
32 |   pure $ unsafeByteString (cast x) buf
33 |
34 | export
35 | bufToByteString : HasIO io => Ptr Buf -> Bits32 -> io ByteString
36 | bufToByteString p x = getBufBase p >>= \y => toByteString y x
37 |
38 | ||| Reads `n` bytes of data from the byte array in a `uv_buf_t`
39 | ||| into an Idris-managed string.
40 | export %inline
41 | toString : HasIO io => Ptr Bits8 -> Bits32 -> io String
42 | toString p s = toString <$> toByteString p s
43 |
44 | ||| Allocates a byte array to hold the data in the given bytestring.
45 | export
46 | fromByteString : HasIO io => ByteString -> io (Ptr Bits8)
47 | fromByteString (BS s (BV b o _)) =
48 |   copyFromBuffer (unsafeGetBuffer b) (cast s) (cast o)
49 |
50 | ||| Allocates a byte array to hold the data in the given bytestring.
51 | export %inline
52 | fromString : HasIO io => String -> io (Ptr Bits8)
53 | fromString = fromByteString . fromString
54 |