J. Abrahamson
J. Abrahamson

Reputation: 74364

How can you best offer a generic interface on bytestrings?

I'd like to offer a "user friendly" generic interface for a library I'm writing. It seems like the thing that ought to already exist, so I'd like to not reinvent the wheel.

At it's heart, the library is a wrapper over a lot of C functions that take unsigned char[]. Right now I'm using vector since it was easy to understand how to use Mutable to allocate and freeze C arrays being written by the library, but the more popular interface would be ByteString. Is there a good way to transparently convert them? And/or a good way to offer a generic interface for both of them?

As an example, I'd like to be able to write a function

doIt :: ByteArrayLike ba => ba -> ba

and have it operate on ByteString and Vector Word8 identically.

Upvotes: 3

Views: 150

Answers (1)

cdk
cdk

Reputation: 6778

I'm not sure what kind of operations you want on ByteString and Vector, but you could take a look at the lens package, which provides generalized operations on both.

Perhaps implement a IsByteString instance for Vector? see here

Upvotes: 1

Related Questions