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
impl Writer
Sourcepub fn with_metadata(metadata: Metadata) -> Self
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());Sourcepub fn with_metadata_and_capacity(metadata: Metadata, capacity: usize) -> Self
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());Sourcepub fn merge_strategy(self, strategy: MergeStrategy) -> Self
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());Sourcepub fn insert<T: Serialize + ?Sized>(
&mut self,
network: IpNetwork,
value: &T,
) -> Result<()>
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());Sourcepub fn insert_encoded<T: MmdbEncode + ?Sized>(
&mut self,
network: IpNetwork,
value: &T,
) -> Result<()>
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());Sourcepub fn insert_entry<T: MmdbRecord + ?Sized>(&mut self, entry: &T) -> Result<()>
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());Sourcepub fn insert_value(&mut self, network: IpNetwork, value: Value) -> Result<()>
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());Sourcepub fn insert_value_arc(
&mut self,
network: IpNetwork,
value: Arc<Value>,
) -> Result<()>
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());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());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());Sourcepub fn insert_batch<I>(&mut self, entries: I) -> Result<()>
pub fn insert_batch<I>(&mut self, entries: I) -> Result<()>
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());Sourcepub fn finish(self) -> Result<Vec<u8>>
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());Sourcepub fn write_to_file(self, path: impl AsRef<Path>) -> Result<()>
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);