diff options
author | Robin Jarry <robin@jarry.cc> | 2022-12-21 11:31:04 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-01-04 22:57:31 +0100 |
commit | 5677f93ff8e0f212be112a110fcfe09663c84f98 (patch) | |
tree | 9f47f430b312d1f0dc1acdd3735cfd76ad21e0f0 /worker/mbox/models.go | |
parent | 36fded03e762da97edde61559c8bf60d5749d6a2 (diff) | |
download | aerc-5677f93ff8e0f212be112a110fcfe09663c84f98.tar.gz |
model: change flags array to bitmask
Using a list of integers is not optimal. Use a bit mask instead.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'worker/mbox/models.go')
-rw-r--r-- | worker/mbox/models.go | 27 |
1 files changed, 8 insertions, 19 deletions
diff --git a/worker/mbox/models.go b/worker/mbox/models.go index eec73379..3af93a84 100644 --- a/worker/mbox/models.go +++ b/worker/mbox/models.go @@ -148,7 +148,7 @@ func (f *container) newUid() (next uint32) { return } -func (f *container) Append(r io.Reader, flags []models.Flag) error { +func (f *container) Append(r io.Reader, flags models.Flags) error { data, err := io.ReadAll(r) if err != nil { return err @@ -164,7 +164,7 @@ func (f *container) Append(r io.Reader, flags []models.Flag) error { // message implements the lib.RawMessage interface type message struct { uid uint32 - flags []models.Flag + flags models.Flags content []byte } @@ -172,7 +172,7 @@ func (m *message) NewReader() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(m.content)), nil } -func (m *message) ModelFlags() ([]models.Flag, error) { +func (m *message) ModelFlags() (models.Flags, error) { return m.flags, nil } @@ -184,22 +184,11 @@ func (m *message) UID() uint32 { return m.uid } -func (m *message) SetFlag(flag models.Flag, state bool) error { - flagSet := make(map[models.Flag]bool) - flags, err := m.ModelFlags() - if err != nil { - return err - } - for _, f := range flags { - flagSet[f] = true - } - flagSet[flag] = state - newFlags := make([]models.Flag, 0) - for flag, isSet := range flagSet { - if isSet { - newFlags = append(newFlags, flag) - } +func (m *message) SetFlag(flag models.Flags, state bool) error { + if state { + m.flags |= flag + } else { + m.flags &^= flag } - m.flags = newFlags return nil } |