<div class="gmail_quote">On Tue, Dec 15, 2009 at 1:39 AM, Simon Marlow <span dir="ltr">&lt;<a href="mailto:marlowsd@gmail.com">marlowsd@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
I haven&#39;t implemented a bytestring-backed Handle, but as you say all the abstractions should be present.  It would be a great thing to have on Hackage.<br>
<br>
A good starting point would be the mmap-backed Handle code that I wrote for my talk at the Haskell Implementors Workshop last year.  I&#39;d intended to polish this up and upload to Hackage, but never got around to it.  I&#39;ve put the code here for now:<br>

<br>
<a href="http://www.haskell.org/~simonmar/mmap-handle.tar.gz" target="_blank">http://www.haskell.org/~simonmar/mmap-handle.tar.gz</a></blockquote><div><br></div><div>Ooh, thanks! I&#39;ll take a look-see.</div><div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Yes, you may remember we talked about this in Edinburgh (the conversion would probably make more sense to you now than it did then :-).<br>
</blockquote><div><br></div><div>I do indeed remember :-)</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
One thing I experimented with is making CharBuffers use UTF-16.  You&#39;ll see some instances of #ifdef CHARBUF_UTF16 in the code - it partially works, I believe the main missing piece is support in the built-in codecs.  I don&#39;t think it would be too hard to fix them, they just need to more abstract about offsets in the CharBuffer; writeCharBuffer/readCharBuffer already handle the UTF-16 encoding/decoding.<br>
</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
So one possibility is to get this working and then avoid the extra copy by just taking out the ByteArray# inside a CharBuffer and turning it into a text buffer. I&#39;m not sure of the details here, but I imagine something along those lines would work.  We would then have to allocate a new CharBuffer for the Handle.<br>
</blockquote><div><br></div><div>Yes, that would amount to double-buffering, and would work nicely, only the current buffers go through foreign pointers while text uses an unpinned array. I can see why this is (so iconv can actually work), but it does introduce a fly into the ointment :-)</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Another possibility is (as you suggested) to make Handles independent of the representation of the CharBuffer, making it completely abstract.  I haven&#39;t put much thought into that, it might well be a better approach.  It would presumably involve a new existential class constraint in the Handle for the CharBuffer operations, and we&#39;d have to be careful about performance: currently I think the CharBuffer operations get inlined nicely.<br>
</blockquote><div><br></div><div>Aye. I think this would have the same problem with foreign transcoding code that wants a reliable pointer.</div></div>