Support duplicating a TChan with the current content available.
I propose adding a function to the Control.Concurrent.STM.TChan that duplicates the channel, but with all current content available in the duplicate channel too. Suggestion:
-- |Clone a 'TChan': similar to dupTChan, but the cloned channel starts with the
-- same content available as the original channel.
cloneTChan :: TChan a -> STM (TChan a)
cloneTChan (TChan read write) = do
readpos <- readTVar read
new_read <- newTVar readpos
return (TChan new_read write)
A little bit of context for the proposal. I was writing a function that started getting a bit complicated (I am waiting of either this message or two other messages in order, and there might be some uninteresting messages before I get any of those...) And I realized that this is a parsing problem and we have libraries for those! But, then I had to realize that there is no way to implement for example Parsec's Stream class with the currently available TChan methods. With the proposed cloneTChan it's trivial:
instance Stream (TChan a) STM a where
uncons chan = do
chan' <- cloneTChan chan
x <- readTChan chan'
return $ Just (x,chan')
And I can imagine some other applications where the new function would come handy.
The same thing also applies to Control.Concurrent.Chan, though there you have other possibilities (eg. you can "convert" a Chan a into an [a] with unsafeInterleaveIO and work on that, which is not a viable option for TChan.)
Trac metadata
| Trac field | Value |
|---|---|
| Version | 7.4.2 |
| Type | FeatureRequest |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | libraries (other) |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture |