Expand description
Hardware Address
§Installation
§Rust
[dependencies]
hardware-address = "0.2"Optional features:
# Serialization support
hardware-address = { version = "0.2", features = ["serde"] }
# arbitrary support
hardware-address = { version = "0.2", features = ["arbitrary"] }
# quickcheck support
hardware-address = { version = "0.2", features = ["quickcheck"] }
# Python bindings
hardware-address = { version = "0.2", features = ["pyo3"] }
# WebAssembly bindings
hardware-address = { version = "0.2", features = ["wasm-bindgen"] }§Python
pip install hardware-address§JavaScript/TypeScript (WASM)
npm install hardware-address§Features
std(default): Standard library supportalloc: Allocation support forno_stdenvironmentsserde: Serialization/deserializationarbitrary: Fuzzing and property-based testing witharbitraryquickcheck: Property-based testing withquickcheckpyo3: Python bindingswasm-bindgen: WebAssembly/JavaScript bindings
§Usage
§Rust
use hardware_address::MacAddr;
use std::str::FromStr;
// Parse from string (supports colon, hyphen, and dot separators)
let addr = MacAddr::from_str("00:00:5e:00:53:01").unwrap();
let addr = MacAddr::from_str("00-00-5e-00-53-01").unwrap();
let addr = MacAddr::from_str("0000.5e00.5301").unwrap();
// Create from bytes
let addr = MacAddr::from_raw([0x00, 0x00, 0x5e, 0x00, 0x53, 0x01]);
// Format conversions
println!("{}", addr); // 00:00:5e:00:53:01 (default: colon-separated)
println!("{}", addr.to_hyphen_separated()); // 00-00-5e-00-53-01
println!("{}", addr.to_dot_separated()); // 0000.5e00.5301
// Access bytes
let bytes: [u8; 6] = addr.octets();
let slice: &[u8] = addr.as_bytes();§Python
from hardware_address import MacAddr
# Parse from string (supports colon, hyphen, and dot separators)
addr = MacAddr.parse("00:00:5e:00:53:01")
addr = MacAddr.parse("00-00-5e-00-53-01")
addr = MacAddr.parse("0000.5e00.5301")
# Create from bytes
addr = MacAddr(b"\x00\x00\x5e\x00\x53\x01")
# String representation
print(str(addr)) # 00:00:5e:00:53:01
print(repr(addr)) # MacAddr("00:00:5e:00:53:01")
# Get bytes
data = bytes(addr)
# Comparison and hashing
if addr1 == addr2:
print("Addresses are equal")
my_dict = {addr: "device1"}§JavaScript/TypeScript
import { MacAddr } from 'hardware-address';
// Parse from string (supports colon, hyphen, and dot separators)
const addr = MacAddr.parse("00:00:5e:00:53:01");
const addrHyphen = MacAddr.parse("00-00-5e-00-53-01");
const addrDot = MacAddr.parse("0000.5e00.5301");
// Create from bytes
const bytes = new Uint8Array([0x00, 0x00, 0x5e, 0x00, 0x53, 0x01]);
const addr = MacAddr.fromBytes(bytes);
// Format conversions
console.log(addr.toString()); // 00:00:5e:00:53:01
console.log(addr.toHyphenSeparated()); // 00-00-5e-00-53-01
console.log(addr.toDotSeparated()); // 0000.5e00.5301
// Get bytes
const bytes = addr.toBytes();§Supported Address Types
MacAddr: 6-byte IEEE 802 MAC-48/EUI-48 addressesEui64Addr: 8-byte EUI-64 addressesInfiniBandAddr: 20-byte IP over InfiniBand link-layer addresses
All types support the same API across all platforms (Rust, Python, and JavaScript).
§Format Support
All address types support parsing and formatting in three standard formats:
| Format | Example |
|---|---|
| Colon-separated | 00:00:5e:00:53:01 |
| Hyphen-separated | 00-00-5e-00-53-01 |
| Dot-separated | 0000.5e00.5301 |
§Pedigree
This code is inspired and modified based on Golang’s mac implementation.
§License
hardware-address is under the terms of both the MIT license and the
Apache License (Version 2.0).
See LICENSE-APACHE, LICENSE-MIT for details.
Copyright (c) 2025 Al Liu.
Macros§
- addr_ty
- A macro for defining address types.
Structs§
- Eui64
Addr - Represents a physical EUI-64 format address.
- Infini
Band Addr - Represents a physical 20-octet InfiniBand format address.
- MacAddr
- Represents a physical hardware address (MAC address).
Enums§
- Parse
Error - ParseError represents an error that occurred while parsing hex address.
Functions§
- parse
- Parses s as an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address and etc using one of the following formats:
- xtoi
- Converts a hexadecimal slice to an integer.
- xtoi2
- Converts the next two hex digits of s into a byte. If s is longer than 2 bytes then the third byte must match e.
Type Aliases§
- Parse
Eui64 Addr Error - Represents an error that occurred while parsing
Eui64Addr. - Parse
Infini Band Addr Error - Represents an error that occurred while parsing
InfiniBandAddr. - Parse
MacAddr Error - Represents an error that occurred while parsing
MacAddr.