{"id":49547121,"url":"https://github.com/dankmeme01/dbuf","last_synced_at":"2026-05-02T20:03:24.841Z","repository":{"id":353827642,"uuid":"1221074511","full_name":"dankmeme01/dbuf","owner":"dankmeme01","description":"Small header-only C++20 library for easy binary serialization","archived":false,"fork":false,"pushed_at":"2026-04-25T19:20:09.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-25T20:15:20.450Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dankmeme01.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-25T18:07:25.000Z","updated_at":"2026-04-25T19:20:04.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/dankmeme01/dbuf","commit_stats":null,"previous_names":["dankmeme01/dbuf"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/dankmeme01/dbuf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankmeme01%2Fdbuf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankmeme01%2Fdbuf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankmeme01%2Fdbuf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankmeme01%2Fdbuf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dankmeme01","download_url":"https://codeload.github.com/dankmeme01/dbuf/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankmeme01%2Fdbuf/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32547653,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T19:18:06.202Z","status":"ssl_error","status_checked_at":"2026-05-02T19:16:21.335Z","response_time":132,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-05-02T20:03:24.340Z","updated_at":"2026-05-02T20:03:24.833Z","avatar_url":"https://github.com/dankmeme01.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dbuf\n\nSmall header-only C++20 library for easy binary serialization. Provided classes:\n\n* `ByteReader` - for reading\n* `ByteWriter` - for writing\n* `CircularByteBuffer` - a collection akin to `std::deque\u003cuint8_t\u003e` but implemented as a ring buffer, providing fast writes at the back and reads at the front.\n\nThe binary buffers are fairly flexible and extensible, they allow creating custom sources/sinks in a fully compile-time fashion. Provided are a few basic sinks and sources that are good enough for most use cases.\n\n## ByteReader\n\n```cpp\n#include \u003cdbuf/ByteReader.hpp\u003e\n\nusing namespace dbuf;\n\nstd::vector\u003cuint8_t\u003e data = { 0x01, 0x02 };\nByteReader reader{data}; // can be made from a span or a vector ref\n\nuint16_t val = reader.readU16().unwrap();\nassert(val == 0x0201); // all values are in little-endian, so swap the bytes!\n\n(void) reader.setPosition(0);\nassert(reader.readU8().unwrap() == 0x01);\nassert(reader.readU8().unwrap() == 0x02);\n```\n\nCustom sources can be created by implementing the `ReadSource` trait:\n```cpp\ntemplate \u003ctypename T\u003e\nconcept ReadSource = requires(T reader, uint8_t* data, size_t size) {\n    { reader.read(data, size) } -\u003e std::same_as\u003cResult\u003cvoid\u003e\u003e;\n};\n```\n\nFor extra functionality (such as seeking), you can implement the following traits on your source as well:\n```cpp\ntemplate \u003ctypename T\u003e\nconcept SeekSource = ReadSource\u003cT\u003e \u0026\u0026 requires(T reader, size_t pos) {\n    { reader.position() } -\u003e std::same_as\u003csize_t\u003e;\n    { reader.setPosition(pos) } -\u003e std::same_as\u003cResult\u003cvoid\u003e\u003e;\n    { reader.totalSize() } -\u003e std::same_as\u003csize_t\u003e;\n};\n\ntemplate \u003ctypename T\u003e\nconcept SkipSource = ReadSource\u003cT\u003e \u0026\u0026 requires(T reader, size_t size) {\n    { reader.skip(size) } -\u003e std::same_as\u003cResult\u003cvoid\u003e\u003e;\n};\n```\n\nFor example, here's a basic custom source that simply generates sequential bytes (wrapping at `0xff`) forever, does not implement `SeekSource` but implements `SkipSource`:\n\n```cpp\nstruct SequentialSource {\n    uint8_t current = 0;\n\n    Result\u003cvoid\u003e read(uint8_t* out, size_t size) {\n        for (size_t i = 0; i \u003c size; ++i) {\n            out[i] = current++;\n        }\n        return Ok();\n    }\n\n    Result\u003cvoid\u003e skip(size_t size) {\n        current += size;\n        return Ok();\n    }\n};\n\n// use our custom SequentialSource instead of the default SpanReadSource\nByteReader reader(SequentialSource{});\n\nassert(reader.readU8().unwrap() == 0);\nassert(reader.readU16().unwrap() == 0x0201);\nassert(reader.skip(3).isOk());\nassert(reader.readU8() == 0x06);\n\n// setPosition() / position() will not compile on this source!\n```\n\n## ByteWriter\n\n```cpp\n#include \u003cdbuf/ByteWriter.hpp\u003e\n\nusing namespace dbuf;\n\nByteWriter wr;\nwr.writeU8(0xff);\nwr.writeU16(0x0102);\nwr.writeBool(true);\n\nauto written = wr.written();\nassert(written.size() == 4);\nassert(written[0] == 0xff);\nassert(written[1] == 0x02);\nassert(written[2] == 0x01);\nassert(written[3] == 0x01);\n```\n\nBy default, `HeapSink` is used, which internally stores all the data in a `std::vector\u003cuint8_t\u003e` and grows as needed, meaning writes can never return an error. For encoding without any allocation, `ArrayByteWriter` can be used, which is simply an alias for `ByteWriter\u003cArraySink\u003cN\u003e\u003e`:\n\n```cpp\nArrayByteWriter\u003c\u003e wr;\n\n// writeU32 and all other write methods now can fail, in case there's not enough space\nauto res = wr.writeU32(42);\nif (!res) {\n    std::println(\"not enough space!\");\n}\n```\n\nYou can also create custom sinks to customize the behavior, by implementing either `TryWriteSink` (if writing can fail) or `WriteSink` (if writing cannot fail):\n\n```cpp\ntemplate \u003ctypename T\u003e\nconcept TryWriteSink = AnyWriteSink\u003cT\u003e \u0026\u0026 requires(T writer, const uint8_t* data, size_t size) {\n    { writer.write(data, size) } -\u003e std::same_as\u003cResult\u003cvoid\u003e\u003e;\n};\n\ntemplate \u003ctypename T\u003e\nconcept WriteSink = AnyWriteSink\u003cT\u003e \u0026\u0026 requires(T writer, const uint8_t* data, size_t size) {\n    { writer.write(data, size) } -\u003e std::same_as\u003cvoid\u003e;\n};\n```\n\nAdditional traits may be implemented for extra functionality:\n```cpp\ntemplate \u003ctypename T\u003e\nconcept WriteZeroesSink = AnyWriteSink\u003cT\u003e \u0026\u0026 requires(T writer, size_t size) {\n    { writer.writeZeroes(size) };\n};\n\ntemplate \u003ctypename T\u003e\nconcept SeekWriteSink = AnyWriteSink\u003cT\u003e \u0026\u0026 requires(T writer, size_t pos, size_t size) {\n    { writer.setPosition(pos) } -\u003e std::same_as\u003cResult\u003cvoid\u003e\u003e;\n    { writer.position() } -\u003e std::same_as\u003csize_t\u003e;\n    { writer.slice(pos, size) } -\u003e std::same_as\u003cstd::span\u003cconst uint8_t\u003e\u003e;\n};\n```\n\nFor example, here's a sink that simply voids all the data, implements `WriteSink` and `WriteZeroesSink`:\n\n```cpp\nstruct VoidSink {\n    void write(const uint8_t* data, size_t size) {}\n    void writeZeroes(size_t size) {}\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankmeme01%2Fdbuf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdankmeme01%2Fdbuf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankmeme01%2Fdbuf/lists"}