简体   繁体   English

你怎么能最好地在字节串上提供通用接口?

[英]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[] . 在它的核心,库是很多C函数的包装器,它们采用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 . 现在我正在使用vector因为很容易理解如何使用Mutable来分配和freeze由库写入的C数组,但更流行的接口是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. 并使它在ByteStringVector Word8相同。

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. 我不确定你想要什么样的操作ByteStringVector ,但你可以看一下lens包,它提供了两者的通用操作。

Perhaps implement a IsByteString instance for Vector ? 也许为Vector实现一个IsByteString实例? see here 看到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM