use dyn $trait
syntax
This commit is contained in:
parent
f5a6ce44c7
commit
9ff91eeffb
8
dnsbox/src/bin/resolver/cache/mod.rs
vendored
8
dnsbox/src/bin/resolver/cache/mod.rs
vendored
@ -20,13 +20,13 @@ pub enum Source {
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
rrset: Vec<Box<RRData>>,
|
rrset: Vec<Box<dyn RRData>>,
|
||||||
source: Source,
|
source: Source,
|
||||||
ttl: u32,
|
ttl: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Entry {
|
impl Entry {
|
||||||
pub fn new(rrset: Vec<Box<RRData>>, source: Source) -> Self {
|
pub fn new(rrset: Vec<Box<dyn RRData>>, source: Source) -> Self {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ impl InnerEntry {
|
|||||||
|
|
||||||
type NameEntry = HashMap<Type, InnerEntry>;
|
type NameEntry = HashMap<Type, InnerEntry>;
|
||||||
|
|
||||||
pub type LookupFunction = Box<FnMut(DnsName) -> Box<Future<Item = Entry, Error = Error>>>;
|
pub type LookupFunction = Box<dyn FnMut(DnsName) -> Box<dyn Future<Item = Entry, Error = Error>>>;
|
||||||
|
|
||||||
pub struct Cache {
|
pub struct Cache {
|
||||||
names: Rc<RefCell<HashMap<String, NameEntry>>>,
|
names: Rc<RefCell<HashMap<String, NameEntry>>>,
|
||||||
@ -139,7 +139,7 @@ impl Cache {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics when the `rrset` entries don't match `rrtype`.
|
/// Panics when the `rrset` entries don't match `rrtype`.
|
||||||
pub fn insert_hint(&self, name: DnsName, rrtype: Type, rrset: Vec<Box<RRData>>) {
|
pub fn insert_hint(&self, name: DnsName, rrtype: Type, rrset: Vec<Box<dyn RRData>>) {
|
||||||
for e in &rrset {
|
for e in &rrset {
|
||||||
assert_eq!(rrtype, e.rr_type());
|
assert_eq!(rrtype, e.rr_type());
|
||||||
}
|
}
|
||||||
|
2
dnsbox/src/bin/resolver/cache/root_hints.rs
vendored
2
dnsbox/src/bin/resolver/cache/root_hints.rs
vendored
@ -21,7 +21,7 @@ static DATA: [(&str, &str, &str); 13] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
pub fn load_hints(cache: &super::Cache) {
|
pub fn load_hints(cache: &super::Cache) {
|
||||||
let mut root_ns_set: Vec<Box<RRData>> = Vec::new();
|
let mut root_ns_set: Vec<Box<dyn RRData>> = Vec::new();
|
||||||
|
|
||||||
for &(name, ipv4, ipv6) in &DATA {
|
for &(name, ipv4, ipv6) in &DATA {
|
||||||
let name = name.parse::<DnsName>().expect("invalid root hint name");
|
let name = name.parse::<DnsName>().expect("invalid root hint name");
|
||||||
|
@ -104,7 +104,7 @@ pub struct Resource {
|
|||||||
pub name: DnsCompressedName,
|
pub name: DnsCompressedName,
|
||||||
pub class: Class,
|
pub class: Class,
|
||||||
pub ttl: u32,
|
pub ttl: u32,
|
||||||
pub data: Box<RRData>,
|
pub data: Box<dyn RRData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DnsPacketData for Resource {
|
impl DnsPacketData for Resource {
|
||||||
|
@ -55,7 +55,7 @@ fn get_first_answer_rdata(packet: Bytes) -> Result<Bytes> {
|
|||||||
Ok(data.get_ref().slice(pos, pos + rdlength))
|
Ok(data.get_ref().slice(pos, pos + rdlength))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialized_answer(rrdata: Box<RRData>) -> Result<Bytes> {
|
fn serialized_answer(rrdata: Box<dyn RRData>) -> Result<Bytes> {
|
||||||
let mut p = DnsPacket{
|
let mut p = DnsPacket{
|
||||||
question: vec![
|
question: vec![
|
||||||
Question {
|
Question {
|
||||||
@ -93,7 +93,7 @@ fn check(q: Type, text_input: &'static str, canonic: Option<&'static str>, raw:
|
|||||||
context.set_record_type(q);
|
context.set_record_type(q);
|
||||||
context.set_last_ttl(3600);
|
context.set_last_ttl(3600);
|
||||||
|
|
||||||
let d_zone: Box<RRData> = text::parse_with(text_input, |data| {
|
let d_zone: Box<dyn RRData> = text::parse_with(text_input, |data| {
|
||||||
registry::parse_rr_data(&context, data)
|
registry::parse_rr_data(&context, data)
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ fn check(q: Type, text_input: &'static str, canonic: Option<&'static str>, raw:
|
|||||||
// pdns tests deserialize `zone_as_wire` (from below) here, but we
|
// pdns tests deserialize `zone_as_wire` (from below) here, but we
|
||||||
// make sure it's the same anyway
|
// make sure it's the same anyway
|
||||||
let d_wire_packet = deserialize_with(fake_packet(q, raw), DnsPacket::deserialize).unwrap();
|
let d_wire_packet = deserialize_with(fake_packet(q, raw), DnsPacket::deserialize).unwrap();
|
||||||
let d_wire: &Box<RRData> = &d_wire_packet.answer[0].data;
|
let d_wire: &Box<dyn RRData> = &d_wire_packet.answer[0].data;
|
||||||
|
|
||||||
let d_wire_text = d_wire.text().unwrap();
|
let d_wire_text = d_wire.text().unwrap();
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ pub fn known_name_to_type(name: &str) -> Option<Type> {
|
|||||||
Some(t)
|
Some(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deserialize_rr_data(ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Box<RRData>> {
|
pub fn deserialize_rr_data(ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Box<dyn RRData>> {
|
||||||
let registry = registry();
|
let registry = registry();
|
||||||
match registry.type_parser.get(&rr_type) {
|
match registry.type_parser.get(&rr_type) {
|
||||||
Some(p) => p.deserialize_rr_data(ttl, rr_class, rr_type, data),
|
Some(p) => p.deserialize_rr_data(ttl, rr_class, rr_type, data),
|
||||||
@ -50,7 +50,7 @@ pub fn deserialize_rr_data(ttl: u32, rr_class: Class, rr_type: Type, data: &mut
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_rr_data(context: &DnsTextContext, data: &mut &str) -> Result<Box<RRData>> {
|
pub fn parse_rr_data(context: &DnsTextContext, data: &mut &str) -> Result<Box<dyn RRData>> {
|
||||||
let registry = registry();
|
let registry = registry();
|
||||||
let t = match context.record_type() {
|
let t = match context.record_type() {
|
||||||
Some(t) => t,
|
Some(t) => t,
|
||||||
@ -80,17 +80,17 @@ trait RRDataTypeParse: 'static {
|
|||||||
TypeId::of::<Self>()
|
TypeId::of::<Self>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_rr_data(&self, ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Box<RRData>>;
|
fn deserialize_rr_data(&self, ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Box<dyn RRData>>;
|
||||||
|
|
||||||
fn parse_rr_data(&self, context: &DnsTextContext, data: &mut &str) -> Result<Box<RRData>>;
|
fn parse_rr_data(&self, context: &DnsTextContext, data: &mut &str) -> Result<Box<dyn RRData>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: RRData + 'static> RRDataTypeParse for TagRRDataType<T> {
|
impl<T: RRData + 'static> RRDataTypeParse for TagRRDataType<T> {
|
||||||
fn deserialize_rr_data(&self, ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Box<RRData>> {
|
fn deserialize_rr_data(&self, ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Box<dyn RRData>> {
|
||||||
T::deserialize_rr_data(ttl, rr_class, rr_type, data).map(|d| Box::new(d) as _)
|
T::deserialize_rr_data(ttl, rr_class, rr_type, data).map(|d| Box::new(d) as _)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_rr_data(&self, context: &DnsTextContext, data: &mut &str) -> Result<Box<RRData>> {
|
fn parse_rr_data(&self, context: &DnsTextContext, data: &mut &str) -> Result<Box<dyn RRData>> {
|
||||||
T::dns_parse_rr_data(context, data).map(|d| Box::new(d) as _)
|
T::dns_parse_rr_data(context, data).map(|d| Box::new(d) as _)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ struct Registry {
|
|||||||
// store (ascii) upper-case names.
|
// store (ascii) upper-case names.
|
||||||
names_to_type: HashMap<Vec<u8>, Type>,
|
names_to_type: HashMap<Vec<u8>, Type>,
|
||||||
type_names: HashMap<Type, String>,
|
type_names: HashMap<Type, String>,
|
||||||
type_parser: HashMap<Type, Box<RRDataTypeParse + Sync>>,
|
type_parser: HashMap<Type, Box<dyn RRDataTypeParse + Sync>>,
|
||||||
// make sure registrations are in order
|
// make sure registrations are in order
|
||||||
prev_type: Option<Type>,
|
prev_type: Option<Type>,
|
||||||
}
|
}
|
||||||
@ -232,7 +232,7 @@ impl Registry {
|
|||||||
|
|
||||||
fn check_registration<T: StaticRRData + Sync + 'static>(&self) {
|
fn check_registration<T: StaticRRData + Sync + 'static>(&self) {
|
||||||
assert_eq!(self.names_to_type.get(T::NAME.as_bytes()), Some(&T::TYPE));
|
assert_eq!(self.names_to_type.get(T::NAME.as_bytes()), Some(&T::TYPE));
|
||||||
let p: &RRDataTypeParse = &**self.type_parser.get(&T::TYPE).expect("no parser registered");
|
let p: &dyn RRDataTypeParse = &**self.type_parser.get(&T::TYPE).expect("no parser registered");
|
||||||
let tid = TypeId::of::<TagRRDataType<T>>();
|
let tid = TypeId::of::<TagRRDataType<T>>();
|
||||||
assert_eq!(p.type_id(), tid);
|
assert_eq!(p.type_id(), tid);
|
||||||
}
|
}
|
||||||
|
@ -90,11 +90,11 @@ impl RRDataText for UnknownRecord {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RRData for UnknownRecord {
|
impl RRData for UnknownRecord {
|
||||||
fn clone_box(&self) -> Box<RRData> {
|
fn clone_box(&self) -> Box<dyn RRData> {
|
||||||
Box::new(self.clone()) as _
|
Box::new(self.clone()) as _
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_any(&self) -> &::std::any::Any {
|
fn as_any(&self) -> &dyn ::std::any::Any {
|
||||||
self as _
|
self as _
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,9 +76,9 @@ impl<T: DnsTextData + StaticRRData> RRDataText for T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait RRData: RRDataPacket + RRDataText + fmt::Debug + 'static {
|
pub trait RRData: RRDataPacket + RRDataText + fmt::Debug + 'static {
|
||||||
fn clone_box(&self) -> Box<RRData>;
|
fn clone_box(&self) -> Box<dyn RRData>;
|
||||||
|
|
||||||
fn as_any(&self) -> &Any;
|
fn as_any(&self) -> &dyn Any;
|
||||||
|
|
||||||
// (type, rrdata)
|
// (type, rrdata)
|
||||||
fn text(&self) -> Result<(String, String)> {
|
fn text(&self) -> Result<(String, String)> {
|
||||||
@ -99,7 +99,7 @@ pub trait RRData: RRDataPacket + RRDataText + fmt::Debug + 'static {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Box<RRData> {
|
impl Clone for Box<dyn RRData> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
self.clone_box()
|
self.clone_box()
|
||||||
}
|
}
|
||||||
|
@ -57,12 +57,12 @@ pub fn escape(data: &[u8]) -> String {
|
|||||||
/// Each call to write!() makes sure a space is emitted to separate it
|
/// Each call to write!() makes sure a space is emitted to separate it
|
||||||
/// from previous fields.
|
/// from previous fields.
|
||||||
pub struct DnsTextFormatter<'a> {
|
pub struct DnsTextFormatter<'a> {
|
||||||
w: &'a mut fmt::Write,
|
w: &'a mut dyn fmt::Write,
|
||||||
need_space: bool,
|
need_space: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> DnsTextFormatter<'a> {
|
impl<'a> DnsTextFormatter<'a> {
|
||||||
pub fn new(w: &'a mut fmt::Write) -> Self {
|
pub fn new(w: &'a mut dyn fmt::Write) -> Self {
|
||||||
DnsTextFormatter {
|
DnsTextFormatter {
|
||||||
w: w,
|
w: w,
|
||||||
need_space: false,
|
need_space: false,
|
||||||
@ -85,7 +85,7 @@ impl<'a> DnsTextFormatter<'a> {
|
|||||||
|
|
||||||
/// direct access to underlying output; you'll need to call
|
/// direct access to underlying output; you'll need to call
|
||||||
/// `next_field` and `end_field` manually.
|
/// `next_field` and `end_field` manually.
|
||||||
pub fn inner(&mut self) -> &mut fmt::Write {
|
pub fn inner(&mut self) -> &mut dyn fmt::Write {
|
||||||
self.w
|
self.w
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,7 +107,7 @@ pub struct DnsTextFormatField<'a: 'b, 'b> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> ::std::ops::Deref for DnsTextFormatField<'a, 'b> {
|
impl<'a, 'b> ::std::ops::Deref for DnsTextFormatField<'a, 'b> {
|
||||||
type Target = fmt::Write + 'a;
|
type Target = dyn fmt::Write + 'a;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
self.inner.w
|
self.inner.w
|
||||||
|
Loading…
Reference in New Issue
Block a user