lpeterse / haskell-socket

A Haskell binding to the POSIX sockets API
MIT License
47 stars 10 forks source link

Ptr-base send/receive functions #41

Closed VyacheslavHashov closed 7 years ago

VyacheslavHashov commented 7 years ago

With these signatures or similar:

send :: Socket f t p -> Ptr Word8 -> Int -> MessageFlags -> IO Int

receive :: Socket f t p -> Ptr Word8 -> Int -> MessageFlags -> IO BS.ByteString

They might be useful in the following scenarios:

VyacheslavHashov commented 7 years ago

There is already System.Socket.Unsafe.

VyacheslavHashov commented 7 years ago

It is a common practice that exposed functions works with Int, not CInt/CSize because they would be more usable. Is it feasible to rewrite all unsafe functions with accordance to this rule?

lpeterse commented 7 years ago

Mhh, open for discussion.

The ByteString library has useAsCStringLen where CStringLen is an alias for (Ptr CChar, CSize). I thought it's not too unusual that one rather has aCSizethan anInt` when dealing with pointers and having allocated the buffer elsewhere.

useAsCStringLen :: ByteString -> (CStringLen -> IO a) -> IO a

VyacheslavHashov commented 7 years ago

As for example, allocating functions in Foreign.ForeignPtr work with Ints. But I have taken your point. If a function already has Ptr in the signature than changing CInt to Int gives no appreciable benefit.

lpeterse commented 7 years ago

One more point for future readers:

CSize is Word64 whereas an Int is only guaranteed to be defined up to 2^29.