Skip to content

Take parsing functions as parameters to avoid need to change library #9

@mangalaman93

Description

@mangalaman93

Currently we have two functions (ReadUint64 and WriteUint64) that are available to convert array of bytes into Uint64. Adding more function requires changes in the library in case of a need of different and more customized conversions.

Given that we do not want to expose the mmaped slice outside the library, this can be solved by taking conversion functions as arguments. This can clearly scope the mmaped array within the library itself.

For examples, consider following piece of code -

type RUint64 func([]byte) (uint64, error)
type WUint64 func([]byte, uint64) error

func (m *Mmap) ReadUint64(offset int, f RUint64) (uint64, error) {
        return f(m.data[offset : offset+8])
}

func (m *Mmap) WriteUint64(offset int, num uint64, f WUint64) {
	return f(m.data[offset:offset+8], num)
}

This is still not extensible to new types without changes to the library though provides customized conversion (such as for little endian architecture, big endian architecture). We can also provide default implementation for these types for direct usability.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions