From: Axel S. <A....@ke...> - 2007-11-08 13:58:57
|
Mon Nov 5 07:06:31 PST 2007 Peter Gavin <pg...@gm...> * glib: add toFlags': like toFlags but works doesn't fail when we haven't defined toEnum for a bit This is needed for a few reasons, the most important of which is for forward compatibility. If a newer version of the upstream package defines a new flag for a flag type, we shouldn't fail just because we don't recognize it. This version allows overlapping flags, for example, given: data OpenFlags = Read | Write | ReadWrite instance Flags OpenFlags instance Enum OpenFlags where ... If ReadWrite is equivalent to [ Read, Write ] in C land, then it will be in Haskell land as well. hunk ./glib/System/Glib/Flags.hs 30 - toFlags + toFlags, + toFlags' hunk ./glib/System/Glib/Flags.hs 34 -import Data.Bits ((.|.), testBit, shiftL, shiftR) +import Data.Bits ((.|.), (.&.), testBit, shiftL, shiftR) +import Data.Maybe (catMaybes) hunk ./glib/System/Glib/Flags.hs 56 +-- * Unlike 'toFlags', this function ignores bits set in the passed +-- 'Int' that do not correspond to a flag. +toFlags' :: Flags a => Int -> [a] +toFlags' n = catMaybes [ if n .&. fromEnum flag == fromEnum flag + then Just flag + else Nothing + | flag <- [ minBound .. maxBound ] ] + |