Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add borrow impl for Dname<Vec<u8>> #219

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/base/name/dname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::traits::{ToDname, ToLabelIter};
use bytes::Bytes;
use core::ops::{Bound, RangeBounds};
use core::str::FromStr;
use core::{cmp, fmt, hash, str};
use core::{borrow, cmp, fmt, hash, str};
use octseq::builder::{EmptyBuilder, FreezeBuilder, FromBuilder, Truncate};
use octseq::octets::{Octets, OctetsFrom};
use octseq::parse::Parser;
Expand Down Expand Up @@ -846,6 +846,27 @@ impl<Octs: AsRef<[u8]> + ?Sized> fmt::Debug for Dname<Octs> {
}
}

//--- Borrow

/// Containers holding `Dname<Vec<u8>>` may be queried either by `Dname<Vec<u8>>` or borrowed
/// forms. This `Borrow` impl supports user code querying containers with compatible-but-different
/// types like the following example:
/// ```
/// use std::collections::HashMap;
///
/// use domain::base::Dname;
///
/// fn get_description(hash: &HashMap<Dname<Vec<u8>>, String>) -> Option<&str> {
/// let lookup_name: &Dname<[u8]> = Dname::from_slice(b"\x03www\x07example\x03com\0").unwrap();
/// hash.get(lookup_name).map(|x| x.as_ref())
/// }
/// ```
impl<Octs: AsRef<[u8]>> borrow::Borrow<Dname<[u8]>> for Dname<Octs> {
fn borrow(&self) -> &Dname<[u8]> {
self.for_slice()
}
}

//--- Serialize and Deserialize

#[cfg(feature = "serde")]
Expand Down