Skip to main content

Writer

Struct Writer 

Source
pub struct Writer { /* private fields */ }
Expand description

In-memory MaxMind DB builder with arena-allocated trie nodes.

The writer uses an arena allocation strategy: all TrieNode instances are stored contiguously in a Vec<TrieNode>, and children are referenced by index instead of Box pointers. This reduces allocation overhead and improves cache locality during tree construction and traversal.

Node indices use u32, limiting the trie to 4 billion nodes (far beyond any practical MMDB database).

Implementations§

Source§

impl Writer

Source

pub fn with_metadata(metadata: Metadata) -> Self

Creates a writer with explicit metadata.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Writer};
let writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
let bytes = writer.finish()?;
assert!(!bytes.is_empty());
Source

pub fn with_metadata_and_capacity(metadata: Metadata, capacity: usize) -> Self

Creates a writer with explicit metadata and pre-allocated node capacity. Capacity reserves trie nodes; it does not limit the number of inserts.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Writer};
let metadata = MetadataBuilder::new().ip_version(4).build()?;
let writer = Writer::with_metadata_and_capacity(metadata, 1024);
assert!(!writer.finish()?.is_empty());
Source

pub fn merge_strategy(self, strategy: MergeStrategy) -> Self

Configures duplicate-network merge behavior. See MergeStrategy for conflict behavior. The setting affects later inserts into the same network.

§Examples
use libmaxminddb_rs::{MergeStrategy, MetadataBuilder, Writer};
let writer = Writer::with_metadata(MetadataBuilder::new().build()?)
    .merge_strategy(MergeStrategy::DeepMerge);
assert!(!writer.finish()?.is_empty());
Source

pub fn insert<T: Serialize + ?Sized>( &mut self, network: IpNetwork, value: &T, ) -> Result<()>

Inserts a serde-serializable value for an IP network. Returns an encoding error for values the MMDB format cannot represent.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Writer};
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
writer.insert("198.51.100.0/24".parse()?, &serde_json::json!({"asn": 64512}))?;
assert!(!writer.finish()?.is_empty());
Source

pub fn insert_encoded<T: MmdbEncode + ?Sized>( &mut self, network: IpNetwork, value: &T, ) -> Result<()>

Inserts a value produced by #[derive(MmdbEncode)].

§Examples
use libmaxminddb_rs::{MetadataBuilder, MmdbEncode, Writer};
#[derive(MmdbEncode)]
struct Record<'a> { category: &'a str }
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
writer.insert_encoded("198.51.100.0/24".parse()?, &Record { category: "example" })?;
assert!(!writer.finish()?.is_empty());
Source

pub fn insert_entry<T: MmdbRecord + ?Sized>(&mut self, entry: &T) -> Result<()>

Inserts a custom record carrying its own #[mmdb(network)] field.

This is the one-object insertion API for types deriving both MmdbEncode and MmdbRecord. The network field is not written into the record data.

§Examples
use libmaxminddb_rs::{IpNetwork, MetadataBuilder, MmdbEncode, MmdbRecord, Writer};
#[derive(MmdbEncode, MmdbRecord)]
struct Record<'a> {
    #[mmdb(network)] network: IpNetwork,
    category: &'a str,
}
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
writer.insert_entry(&Record {
    network: "198.51.100.0/24".parse()?, category: "example",
})?;
assert!(!writer.finish()?.is_empty());
Source

pub fn insert_value(&mut self, network: IpNetwork, value: Value) -> Result<()>

Inserts an already-typed MMDB value. Returns an error when the network family does not match metadata.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Value, Writer};
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
writer.insert_value("198.51.100.0/24".parse()?, Value::Utf8("example".into()))?;
assert!(!writer.finish()?.is_empty());
Source

pub fn insert_value_arc( &mut self, network: IpNetwork, value: Arc<Value>, ) -> Result<()>

Inserts a shared MMDB value without copying the underlying structure.

Alias for insert_value_shared.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Value, Writer};
use std::sync::Arc;
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
let value = Arc::new(Value::Uint32(64512));
writer.insert_value_arc("198.51.100.0/24".parse()?, value)?;
assert!(!writer.finish()?.is_empty());
Source

pub fn insert_value_shared( &mut self, network: IpNetwork, value: Arc<Value>, ) -> Result<()>

Inserts a shared MMDB value without copying the underlying structure. The writer retains the Arc on a first insert; merging may clone the underlying value.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Value, Writer};
use std::sync::Arc;
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
let value = Arc::new(Value::Uint32(64512));
writer.insert_value_shared("198.51.100.0/24".parse()?, value)?;
assert!(!writer.finish()?.is_empty());
Source

pub fn insert_batch_shared<I>( &mut self, networks: I, value: &Arc<Value>, ) -> Result<()>
where I: IntoIterator<Item = IpNetwork>,

Inserts a batch of IP networks sharing the same value. A network-family mismatch aborts the batch with an error.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Value, Writer};
use std::sync::Arc;
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
let networks = ["198.51.100.0/24".parse()?, "203.0.113.0/24".parse()?];
writer.insert_batch_shared(networks, &Arc::new(Value::Uint32(64512)))?;
assert!(!writer.finish()?.is_empty());
Source

pub fn insert_batch<I>(&mut self, entries: I) -> Result<()>
where I: IntoIterator<Item = (IpNetwork, Value)>,

Inserts a batch of network/value pairs. The batch stops at the first invalid entry.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Value, Writer};
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
writer.insert_batch([("198.51.100.0/24".parse()?, Value::Bool(true))])?;
assert!(!writer.finish()?.is_empty());
Source

pub fn finish(self) -> Result<Vec<u8>>

Finalizes the database entirely in memory. Returns encoding errors for oversized trees or unrepresentable values. The returned vector owns the complete MMDB file.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Writer};
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
writer.insert("198.51.100.0/24".parse()?, &serde_json::json!({"asn": 64512}))?;
let bytes = writer.finish()?;
assert!(!bytes.is_empty());
Source

pub fn write_to_file(self, path: impl AsRef<Path>) -> Result<()>

Finalizes and writes a database to disk. Propagates serialization and file I/O errors.

§Examples
use libmaxminddb_rs::{MetadataBuilder, Writer};
let mut writer = Writer::with_metadata(MetadataBuilder::new().ip_version(4).build()?);
writer.insert("198.51.100.0/24".parse()?, &serde_json::json!({"asn": 64512}))?;
let file = tempfile::NamedTempFile::new()?;
writer.write_to_file(file.path())?;
assert!(std::fs::metadata(file.path())?.len() > 0);

Trait Implementations§

Source§

impl Debug for Writer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.