Skip to content

Commit

Permalink
Merge pull request #52 from elrafoon/ser_as_str
Browse files Browse the repository at this point in the history
Add ser_as_str feature to serialize using serialize_str()
  • Loading branch information
krisprice authored Oct 22, 2023
2 parents 6155c21 + 5d21106 commit 585bcad
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ipnet"
version = "2.8.0" # Remember to update html_root_url
version = "2.9.0" # Remember to update html_root_url
authors = ["Kris Price <kris@krisprice.nz>"]
description = "Provides types and useful methods for working with IPv4 and IPv6 network addresses, commonly called IP prefixes. The new `IpNet`, `Ipv4Net`, and `Ipv6Net` types build on the existing `IpAddr`, `Ipv4Addr`, and `Ipv6Addr` types already provided in Rust's standard library and align to their design to stay consistent. The module also provides useful traits that extend `Ipv4Addr` and `Ipv6Addr` with methods for `Add`, `Sub`, `BitAnd`, and `BitOr` operations. The module only uses stable feature so it is guaranteed to compile using the stable toolchain."
license = "MIT OR Apache-2.0"
Expand All @@ -16,10 +16,12 @@ default = ["std"]
std = []
# Implements "schemars::JsonSchema". Also implies "serde".
json = ["serde", "schemars"]
ser_as_str = ["heapless"]

[dependencies]
serde = { package = "serde", version = "1", features = ["derive"], optional = true, default-features=false }
schemars = { version = "0.8", optional = true }
heapless = { version = "*", optional = true }

[dev-dependencies]
serde_test = "1"
Expand Down
4 changes: 4 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Releases

## Version 2.9.0

* Add ser_as_str feature to serialize using serialize_str() #52

## Version 2.8.0

* Add no_std support on nightly #51
Expand Down
14 changes: 14 additions & 0 deletions src/ipnet_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ impl Serialize for Ipv4Net {
where S: Serializer
{
if serializer.is_human_readable() {
#[cfg(feature = "ser_as_str")]
{
let mut buf = heapless::String::<18>::new();
fmt::write(&mut buf, format_args!("{self}")).unwrap();
serializer.serialize_str(&buf)
}
#[cfg(not(feature = "ser_as_str"))]
serializer.collect_str(self)
} else {
let mut seq = serializer.serialize_tuple(5)?;
Expand Down Expand Up @@ -130,6 +137,13 @@ impl Serialize for Ipv6Net {
where S: Serializer
{
if serializer.is_human_readable() {
#[cfg(feature = "ser_as_str")]
{
let mut buf = heapless::String::<43>::new();
fmt::write(&mut buf, format_args!("{self}")).unwrap();
serializer.serialize_str(&buf)
}
#[cfg(not(feature = "ser_as_str"))]
serializer.collect_str(self)
} else {
let mut seq = serializer.serialize_tuple(17)?;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![doc(html_root_url = "https://docs.rs/ipnet/2.8.0")]
#![doc(html_root_url = "https://docs.rs/ipnet/2.9.0")]
//! Types for IPv4 and IPv6 network addresses.
//!
//! This module provides types and useful methods for working with IPv4
Expand Down

0 comments on commit 585bcad

Please sign in to comment.