2 Commits

Author SHA1 Message Date
Stefan Bühler 2ee16d7994 fix field index in proc derive 2 years ago
Stefan Bühler 9ff91eeffb use `dyn $trait` syntax 2 years ago
  1. 8
      dnsbox/src/bin/resolver/cache/mod.rs
  2. 2
      dnsbox/src/bin/resolver/cache/root_hints.rs
  3. 2
      lib/dnsbox-base/src/packet/mod.rs
  4. 6
      lib/dnsbox-base/src/records/powerdns_tests.rs
  5. 16
      lib/dnsbox-base/src/records/registry.rs
  6. 4
      lib/dnsbox-base/src/records/unknown.rs
  7. 6
      lib/dnsbox-base/src/ser/rrdata.rs
  8. 8
      lib/dnsbox-base/src/ser/text/mod.rs
  9. 1
      lib/dnsbox-derive/src/dns_packet_data.rs
  10. 1
      lib/dnsbox-derive/src/dns_text_data.rs

8
dnsbox/src/bin/resolver/cache/mod.rs

@ -20,13 +20,13 @@ pub enum Source {
#[derive(Clone, Debug)]
pub struct Entry {
rrset: Vec<Box<RRData>>,
rrset: Vec<Box<dyn RRData>>,
source: Source,
ttl: u32,
}
impl Entry {
pub fn new(rrset: Vec<Box<RRData>>, source: Source) -> Self {
pub fn new(rrset: Vec<Box<dyn RRData>>, source: Source) -> Self {
unimplemented!()
}
@ -95,7 +95,7 @@ impl 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 {
names: Rc<RefCell<HashMap<String, NameEntry>>>,
@ -139,7 +139,7 @@ impl Cache {
/// # Panics
///
/// 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 {
assert_eq!(rrtype, e.rr_type());
}

2
dnsbox/src/bin/resolver/cache/root_hints.rs

@ -21,7 +21,7 @@ static DATA: [(&str, &str, &str); 13] = [
];
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 {
let name = name.parse::<DnsName>().expect("invalid root hint name");

2
lib/dnsbox-base/src/packet/mod.rs

@ -104,7 +104,7 @@ pub struct Resource {
pub name: DnsCompressedName,
pub class: Class,
pub ttl: u32,
pub data: Box<RRData>,
pub data: Box<dyn RRData>,
}
impl DnsPacketData for Resource {

6
lib/dnsbox-base/src/records/powerdns_tests.rs

@ -55,7 +55,7 @@ fn get_first_answer_rdata(packet: Bytes) -> Result<Bytes> {
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{
question: vec![
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_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)
}).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
// make sure it's the same anyway
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();

16
lib/dnsbox-base/src/records/registry.rs

@ -42,7 +42,7 @@ pub fn known_name_to_type(name: &str) -> Option<Type> {
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();
match registry.type_parser.get(&rr_type) {
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 t = match context.record_type() {
Some(t) => t,
@ -80,17 +80,17 @@ trait RRDataTypeParse: 'static {
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> {
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 _)
}
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 _)
}
}
@ -99,7 +99,7 @@ struct Registry {
// store (ascii) upper-case names.
names_to_type: HashMap<Vec<u8>, Type>,
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
prev_type: Option<Type>,
}
@ -232,7 +232,7 @@ impl Registry {
fn check_registration<T: StaticRRData + Sync + 'static>(&self) {
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>>();
assert_eq!(p.type_id(), tid);
}

4
lib/dnsbox-base/src/records/unknown.rs

@ -90,11 +90,11 @@ impl RRDataText for UnknownRecord {
}
impl RRData for UnknownRecord {
fn clone_box(&self) -> Box<RRData> {
fn clone_box(&self) -> Box<dyn RRData> {
Box::new(self.clone()) as _
}
fn as_any(&self) -> &::std::any::Any {
fn as_any(&self) -> &dyn ::std::any::Any {
self as _
}
}

6
lib/dnsbox-base/src/ser/rrdata.rs

@ -76,9 +76,9 @@ impl<T: DnsTextData + StaticRRData> RRDataText for T {
}
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)
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 {
self.clone_box()
}

8
lib/dnsbox-base/src/ser/text/mod.rs

@ -57,12 +57,12 @@ pub fn escape(data: &[u8]) -> String {
/// Each call to write!() makes sure a space is emitted to separate it
/// from previous fields.
pub struct DnsTextFormatter<'a> {
w: &'a mut fmt::Write,
w: &'a mut dyn fmt::Write,
need_space: bool,
}
impl<'a> DnsTextFormatter<'a> {
pub fn new(w: &'a mut fmt::Write) -> Self {
pub fn new(w: &'a mut dyn fmt::Write) -> Self {
DnsTextFormatter {
w: w,
need_space: false,
@ -85,7 +85,7 @@ impl<'a> DnsTextFormatter<'a> {
/// direct access to underlying output; you'll need to call
/// `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
}
@ -107,7 +107,7 @@ pub struct DnsTextFormatField<'a: 'b, '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 {
self.inner.w

1
lib/dnsbox-derive/src/dns_packet_data.rs

@ -54,6 +54,7 @@ fn derive_unnamed(s: &synstructure::Structure, fields: &syn::FieldsUnnamed) -> T
let mut parse_fields = quote!{};
let mut serialize_fields = quote!{};
for field in 0..fields.unnamed.len() {
let field = syn::Index::from(field);
parse_fields.extend(quote!{
DnsPacketData::deserialize(_data)
.with_context(|e| format!("failed parsing field {}::{}: {}", stringify!(#name), #field, e))?,

1
lib/dnsbox-derive/src/dns_text_data.rs

@ -53,6 +53,7 @@ fn derive_unnamed(s: &synstructure::Structure, fields: &syn::FieldsUnnamed) -> T
let mut parse_fields = quote!{};
let mut format_fields = quote!{};
for field in 0..fields.unnamed.len() {
let field = syn::Index::from(field);
parse_fields.extend(quote!{
DnsTextData::dns_parse(_context, _data)
.with_context(|e| format!("failed parsing field {}::{}: {}", stringify!(#name), #field, e))?,

Loading…
Cancel
Save