BSON_INIT_STATIC(3)

πŸ’… BSON_INIT_STATIC(3) β€” the libbson one, bestie πŸŽ€

Okay so picture this: it's a libbson manual page, it's about bson_init_static, and it's literally the ✨ zero-copy ✨ girlie of the initialize-the-bson family. Let's get into it 🫢


✨ SYNOPSIS ✨

bool
bson_init_static (bson_t *b, const uint8_t *data, size_t length);

That's the whole vibe check: it hands you back a bool, takes a bson_t *, a const uint8_t *, and a size_t. Clean. Iconic. No notes πŸ’…


🧚 PARAMETERS 🧚

  • b β€” a bson_t. Yeah. That one. The thing you're initializing. 🀍
  • data β€” a buffer to initialize with. This is the raw BSON bytes, the moment, the main character.
  • length β€” the length of data in bytes. Count 'em. Don't lie to me about 'em. πŸ”₯

πŸ¦‹ DESCRIPTION πŸ¦‹

So bson_init_static() shall initialize a read-only bson_t on the stack, using the data you provided. And here's the part where I need you to lean in, bestie: no copies of the data will be made. 🫒 None. Zero. Therefore the data must remain valid for the lifetime of the bson_t. Like, the whole lifetime. Not just the cute part. If you free that buffer while the bson_t is still out here living its life, that's on you and it is not going to be a slay πŸ’€

The resulting bson_t has internal references and therefore must not be copied β€” because copying it would leave dangling pointers in the copy. So do not copy it. I'm so serious. πŸ™ˆ

And one more thing: it is an error if length does not match the length embedded in the first four bytes of the BSON data in data. The first four bytes said one thing, you said another, and now we're in our error era. ⚠️ They have to match. Periodt.

🚨 Recap for the girlies in the back: read-only, stack-allocated, zero copies, buffer stays alive forever, no copying the bson_t, and length has to agree with those first four bytes. Do all that and you ate. πŸ‘‘

🌈 RETURNS 🌈

Returns true if the bson_t was successfully initialized, otherwise false. The function can fail if data or length are invalid. So like, check the return value. Be a responsible icon. πŸ’–


πŸ“ AUTHOR πŸ“

MongoDB, Inc β€” yes, that MongoDB. Authors of the whole libbson situation. Respect. 🫑


2009-present, MongoDB, Inc. β€” the full run, no gaps, we love a consistent queen.


πŸ’œ And that's the page!! Written by your girl for libbson 2.5.6, dated Oct 09, 2026 β€” mwah, go forth and initialize statically, bestie, and remember: don't copy the bson_t πŸŽ€βœ¨
πŸ“¦ where this page comes from