diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index d57cdad3..017bf4d3 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @nervosnetwork/ckb-dev @driftluo @TheWaWaR +* @nervosnetwork/ckb-dev @driftluo @eval-exec diff --git a/Makefile b/Makefile index d7c4a8ab..8e892aa7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ MOLC := moleculec -MOLC_VERSION := 0.7.0 +MOLC_VERSION := 0.8.0 MOL_FILES := \ tentacle/src/protocol_select/protocol_select.mol \ diff --git a/secio/src/handshake/handshake_mol.rs b/secio/src/handshake/handshake_mol.rs index a1a2f906..cff379ea 100644 --- a/secio/src/handshake/handshake_mol.rs +++ b/secio/src/handshake/handshake_mol.rs @@ -1,4 +1,4 @@ -// Generated by Molecule 0.7.0 +// Generated by Molecule 0.8.0 use molecule::prelude::*; #[derive(Clone)] @@ -26,14 +26,15 @@ impl ::core::fmt::Display for Secp256k1 { } impl ::core::default::Default for Secp256k1 { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - Secp256k1::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + Secp256k1::new_unchecked(v) } } impl Secp256k1 { + const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0]; pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -114,7 +115,7 @@ impl<'r> ::core::fmt::Display for Secp256k1Reader<'r> { impl<'r> Secp256k1Reader<'r> { pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -173,7 +174,7 @@ impl<'r> molecule::prelude::Reader<'r> for Secp256k1Reader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct Secp256k1Builder(pub(crate) Vec); impl Secp256k1Builder { pub const ITEM_SIZE: usize = 1; @@ -181,16 +182,25 @@ impl Secp256k1Builder { self.0 = v; self } - pub fn push(mut self, v: Byte) -> Self { - self.0.push(v); + pub fn push(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.0.push(v.into()); self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } + pub fn replace(&mut self, index: usize, v: T) -> Option + where + T: ::core::convert::Into, + { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v.into())) + } } impl molecule::prelude::Builder for Secp256k1Builder { type Entity = Secp256k1; @@ -198,7 +208,7 @@ impl molecule::prelude::Builder for Secp256k1Builder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; for inner in &self.0[..] { writer.write_all(inner.as_slice())?; @@ -238,6 +248,30 @@ impl ::core::iter::IntoIterator for Secp256k1 { Secp256k1Iterator(self, 0, len) } } +impl ::core::iter::FromIterator for Secp256k1 { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for Secp256k1 { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} +impl ::core::iter::FromIterator for Secp256k1 { + fn from_iter>(iter: T) -> Self { + Self::new_builder() + .extend(iter.into_iter().map(Into::into)) + .build() + } +} +impl From> for Secp256k1 { + fn from(v: Vec) -> Self { + Self::new_builder() + .set(v.into_iter().map(Into::into).collect()) + .build() + } +} #[derive(Clone)] pub struct Bytes(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Bytes { @@ -263,14 +297,15 @@ impl ::core::fmt::Display for Bytes { } impl ::core::default::Default for Bytes { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - Bytes::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + Bytes::new_unchecked(v) } } impl Bytes { + const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0]; pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -351,7 +386,7 @@ impl<'r> ::core::fmt::Display for BytesReader<'r> { impl<'r> BytesReader<'r> { pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -410,7 +445,7 @@ impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct BytesBuilder(pub(crate) Vec); impl BytesBuilder { pub const ITEM_SIZE: usize = 1; @@ -418,16 +453,25 @@ impl BytesBuilder { self.0 = v; self } - pub fn push(mut self, v: Byte) -> Self { - self.0.push(v); + pub fn push(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.0.push(v.into()); self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } + pub fn replace(&mut self, index: usize, v: T) -> Option + where + T: ::core::convert::Into, + { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v.into())) + } } impl molecule::prelude::Builder for BytesBuilder { type Entity = Bytes; @@ -435,7 +479,7 @@ impl molecule::prelude::Builder for BytesBuilder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; for inner in &self.0[..] { writer.write_all(inner.as_slice())?; @@ -475,6 +519,30 @@ impl ::core::iter::IntoIterator for Bytes { BytesIterator(self, 0, len) } } +impl ::core::iter::FromIterator for Bytes { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for Bytes { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} +impl ::core::iter::FromIterator for Bytes { + fn from_iter>(iter: T) -> Self { + Self::new_builder() + .extend(iter.into_iter().map(Into::into)) + .build() + } +} +impl From> for Bytes { + fn from(v: Vec) -> Self { + Self::new_builder() + .set(v.into_iter().map(Into::into).collect()) + .build() + } +} #[derive(Clone)] pub struct String(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for String { @@ -500,14 +568,15 @@ impl ::core::fmt::Display for String { } impl ::core::default::Default for String { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - String::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + String::new_unchecked(v) } } impl String { + const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0]; pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -588,7 +657,7 @@ impl<'r> ::core::fmt::Display for StringReader<'r> { impl<'r> StringReader<'r> { pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -647,7 +716,7 @@ impl<'r> molecule::prelude::Reader<'r> for StringReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct StringBuilder(pub(crate) Vec); impl StringBuilder { pub const ITEM_SIZE: usize = 1; @@ -655,16 +724,25 @@ impl StringBuilder { self.0 = v; self } - pub fn push(mut self, v: Byte) -> Self { - self.0.push(v); + pub fn push(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.0.push(v.into()); self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } + pub fn replace(&mut self, index: usize, v: T) -> Option + where + T: ::core::convert::Into, + { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v.into())) + } } impl molecule::prelude::Builder for StringBuilder { type Entity = String; @@ -672,7 +750,7 @@ impl molecule::prelude::Builder for StringBuilder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; for inner in &self.0[..] { writer.write_all(inner.as_slice())?; @@ -712,6 +790,30 @@ impl ::core::iter::IntoIterator for String { StringIterator(self, 0, len) } } +impl ::core::iter::FromIterator for String { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for String { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} +impl ::core::iter::FromIterator for String { + fn from_iter>(iter: T) -> Self { + Self::new_builder() + .extend(iter.into_iter().map(Into::into)) + .build() + } +} +impl From> for String { + fn from(v: Vec) -> Self { + Self::new_builder() + .set(v.into_iter().map(Into::into).collect()) + .build() + } +} #[derive(Clone)] pub struct PublicKey(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for PublicKey { @@ -737,11 +839,12 @@ impl ::core::fmt::Display for PublicKey { } impl ::core::default::Default for PublicKey { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0]; - PublicKey::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + PublicKey::new_unchecked(v) } } impl PublicKey { + const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0]; pub const ITEMS_COUNT: usize = 1; pub fn item_id(&self) -> molecule::Number { molecule::unpack_number(self.as_slice()) @@ -845,7 +948,7 @@ impl<'r> molecule::prelude::Reader<'r> for PublicKeyReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct PublicKeyBuilder(pub(crate) PublicKeyUnion); impl PublicKeyBuilder { pub const ITEMS_COUNT: usize = 1; @@ -863,7 +966,7 @@ impl molecule::prelude::Builder for PublicKeyBuilder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE + self.0.as_slice().len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { writer.write_all(&molecule::pack_number(self.0.item_id()))?; writer.write_all(self.0.as_slice()) } @@ -975,6 +1078,11 @@ impl<'r> PublicKeyUnionReader<'r> { } } } +impl From for PublicKey { + fn from(value: Secp256k1) -> Self { + Self::new_builder().set(value).build() + } +} #[derive(Clone)] pub struct Propose(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Propose { @@ -1008,14 +1116,15 @@ impl ::core::fmt::Display for Propose { } impl ::core::default::Default for Propose { fn default() -> Self { - let v: Vec = vec![ - 44, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - Propose::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + Propose::new_unchecked(v) } } impl Propose { + const DEFAULT_VALUE: [u8; 44] = [ + 44, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; pub const FIELD_COUNT: usize = 5; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -1207,9 +1316,6 @@ impl<'r> molecule::prelude::Reader<'r> for ProposeReader<'r> { if slice_len != total_size { return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } if slice_len < molecule::NUMBER_SIZE * 2 { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); } @@ -1242,7 +1348,7 @@ impl<'r> molecule::prelude::Reader<'r> for ProposeReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct ProposeBuilder { pub(crate) rand: Bytes, pub(crate) pubkey: Bytes, @@ -1252,24 +1358,39 @@ pub struct ProposeBuilder { } impl ProposeBuilder { pub const FIELD_COUNT: usize = 5; - pub fn rand(mut self, v: Bytes) -> Self { - self.rand = v; + pub fn rand(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.rand = v.into(); self } - pub fn pubkey(mut self, v: Bytes) -> Self { - self.pubkey = v; + pub fn pubkey(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.pubkey = v.into(); self } - pub fn exchanges(mut self, v: String) -> Self { - self.exchanges = v; + pub fn exchanges(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.exchanges = v.into(); self } - pub fn ciphers(mut self, v: String) -> Self { - self.ciphers = v; + pub fn ciphers(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.ciphers = v.into(); self } - pub fn hashes(mut self, v: String) -> Self { - self.hashes = v; + pub fn hashes(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.hashes = v.into(); self } } @@ -1284,7 +1405,7 @@ impl molecule::prelude::Builder for ProposeBuilder { + self.ciphers.as_slice().len() + self.hashes.as_slice().len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); offsets.push(total_size); @@ -1345,13 +1466,14 @@ impl ::core::fmt::Display for Exchange { } impl ::core::default::Default for Exchange { fn default() -> Self { - let v: Vec = vec![ - 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - Exchange::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + Exchange::new_unchecked(v) } } impl Exchange { + const DEFAULT_VALUE: [u8; 20] = [ + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; pub const FIELD_COUNT: usize = 2; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -1501,9 +1623,6 @@ impl<'r> molecule::prelude::Reader<'r> for ExchangeReader<'r> { if slice_len != total_size { return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } if slice_len < molecule::NUMBER_SIZE * 2 { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); } @@ -1533,19 +1652,25 @@ impl<'r> molecule::prelude::Reader<'r> for ExchangeReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct ExchangeBuilder { pub(crate) epubkey: Bytes, pub(crate) signature: Bytes, } impl ExchangeBuilder { pub const FIELD_COUNT: usize = 2; - pub fn epubkey(mut self, v: Bytes) -> Self { - self.epubkey = v; + pub fn epubkey(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.epubkey = v.into(); self } - pub fn signature(mut self, v: Bytes) -> Self { - self.signature = v; + pub fn signature(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.signature = v.into(); self } } @@ -1557,7 +1682,7 @@ impl molecule::prelude::Builder for ExchangeBuilder { + self.epubkey.as_slice().len() + self.signature.as_slice().len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); offsets.push(total_size); diff --git a/tentacle/Cargo.toml b/tentacle/Cargo.toml index 5d7fda85..e54d6c66 100644 --- a/tentacle/Cargo.toml +++ b/tentacle/Cargo.toml @@ -39,7 +39,7 @@ multiaddr = { path = "../multiaddr", package = "tentacle-multiaddr", version = " molecule = "0.8.0" # upnp -igd = { version = "0.12", optional = true } +igd = { version = "0.15", optional = true, package = "igd-next" } #tls tokio-rustls = { version = "0.26.0", optional = true } diff --git a/tentacle/src/protocol_select/protocol_select_mol.rs b/tentacle/src/protocol_select/protocol_select_mol.rs index 79065e8a..b1068dbc 100644 --- a/tentacle/src/protocol_select/protocol_select_mol.rs +++ b/tentacle/src/protocol_select/protocol_select_mol.rs @@ -1,4 +1,4 @@ -// Generated by Molecule 0.7.0 +// Generated by Molecule 0.8.0 use molecule::prelude::*; #[derive(Clone)] @@ -26,14 +26,15 @@ impl ::core::fmt::Display for String { } impl ::core::default::Default for String { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0]; - String::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + String::new_unchecked(v) } } impl String { + const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0]; pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -114,7 +115,7 @@ impl<'r> ::core::fmt::Display for StringReader<'r> { impl<'r> StringReader<'r> { pub const ITEM_SIZE: usize = 1; pub fn total_size(&self) -> usize { - molecule::NUMBER_SIZE * (self.item_count() + 1) + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() } pub fn item_count(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -173,7 +174,7 @@ impl<'r> molecule::prelude::Reader<'r> for StringReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct StringBuilder(pub(crate) Vec); impl StringBuilder { pub const ITEM_SIZE: usize = 1; @@ -181,16 +182,25 @@ impl StringBuilder { self.0 = v; self } - pub fn push(mut self, v: Byte) -> Self { - self.0.push(v); + pub fn push(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.0.push(v.into()); self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } + pub fn replace(&mut self, index: usize, v: T) -> Option + where + T: ::core::convert::Into, + { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v.into())) + } } impl molecule::prelude::Builder for StringBuilder { type Entity = String; @@ -198,7 +208,7 @@ impl molecule::prelude::Builder for StringBuilder { fn expected_length(&self) -> usize { molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; for inner in &self.0[..] { writer.write_all(inner.as_slice())?; @@ -238,6 +248,30 @@ impl ::core::iter::IntoIterator for String { StringIterator(self, 0, len) } } +impl ::core::iter::FromIterator for String { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for String { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} +impl ::core::iter::FromIterator for String { + fn from_iter>(iter: T) -> Self { + Self::new_builder() + .extend(iter.into_iter().map(Into::into)) + .build() + } +} +impl From> for String { + fn from(v: Vec) -> Self { + Self::new_builder() + .set(v.into_iter().map(Into::into).collect()) + .build() + } +} #[derive(Clone)] pub struct StringVec(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for StringVec { @@ -269,11 +303,12 @@ impl ::core::fmt::Display for StringVec { } impl ::core::default::Default for StringVec { fn default() -> Self { - let v: Vec = vec![4, 0, 0, 0]; - StringVec::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + StringVec::new_unchecked(v) } } impl StringVec { + const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0]; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize } @@ -460,23 +495,32 @@ impl<'r> molecule::prelude::Reader<'r> for StringVecReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct StringVecBuilder(pub(crate) Vec); impl StringVecBuilder { pub fn set(mut self, v: Vec) -> Self { self.0 = v; self } - pub fn push(mut self, v: String) -> Self { - self.0.push(v); + pub fn push(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.0.push(v.into()); self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } + pub fn replace(&mut self, index: usize, v: T) -> Option + where + T: ::core::convert::Into, + { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v.into())) + } } impl molecule::prelude::Builder for StringVecBuilder { type Entity = StringVec; @@ -489,7 +533,7 @@ impl molecule::prelude::Builder for StringVecBuilder { .map(|inner| inner.as_slice().len()) .sum::() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { let item_count = self.0.len(); if item_count == 0 { writer.write_all(&molecule::pack_number( @@ -572,6 +616,16 @@ impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for StringVecReaderIterator<'t, self.2 - self.1 } } +impl ::core::iter::FromIterator for StringVec { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for StringVec { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} #[derive(Clone)] pub struct ProtocolInfo(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for ProtocolInfo { @@ -602,13 +656,14 @@ impl ::core::fmt::Display for ProtocolInfo { } impl ::core::default::Default for ProtocolInfo { fn default() -> Self { - let v: Vec = vec![ - 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, - ]; - ProtocolInfo::new_unchecked(v.into()) + let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE); + ProtocolInfo::new_unchecked(v) } } impl ProtocolInfo { + const DEFAULT_VALUE: [u8; 20] = [ + 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, + ]; pub const FIELD_COUNT: usize = 2; pub fn total_size(&self) -> usize { molecule::unpack_number(self.as_slice()) as usize @@ -758,9 +813,6 @@ impl<'r> molecule::prelude::Reader<'r> for ProtocolInfoReader<'r> { if slice_len != total_size { return ve!(Self, TotalSizeNotMatch, total_size, slice_len); } - if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 { - return Ok(()); - } if slice_len < molecule::NUMBER_SIZE * 2 { return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len); } @@ -790,19 +842,25 @@ impl<'r> molecule::prelude::Reader<'r> for ProtocolInfoReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct ProtocolInfoBuilder { pub(crate) name: String, pub(crate) support_versions: StringVec, } impl ProtocolInfoBuilder { pub const FIELD_COUNT: usize = 2; - pub fn name(mut self, v: String) -> Self { - self.name = v; + pub fn name(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.name = v.into(); self } - pub fn support_versions(mut self, v: StringVec) -> Self { - self.support_versions = v; + pub fn support_versions(mut self, v: T) -> Self + where + T: ::core::convert::Into, + { + self.support_versions = v.into(); self } } @@ -814,7 +872,7 @@ impl molecule::prelude::Builder for ProtocolInfoBuilder { + self.name.as_slice().len() + self.support_versions.as_slice().len() } - fn write(&self, writer: &mut W) -> ::molecule::io::Result<()> { + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1); let mut offsets = Vec::with_capacity(Self::FIELD_COUNT); offsets.push(total_size); diff --git a/tentacle/src/upnp/mod.rs b/tentacle/src/upnp/mod.rs index aba08a00..abcf20dd 100644 --- a/tentacle/src/upnp/mod.rs +++ b/tentacle/src/upnp/mod.rs @@ -48,7 +48,7 @@ impl IgdClient { } Ok(gateway) => { // if gateway address is public, don't need upnp, disable it - if is_reachable((*gateway.addr.ip()).into()) { + if is_reachable((gateway.addr.ip()).into()) { return None; } @@ -73,7 +73,11 @@ impl IgdClient { let state = get_local_net_state().ok().and_then(|networks| { networks.into_iter().find(|network| { - in_same_subnet(network.address, *gateway.addr.ip(), network.net_mask) + if let std::net::IpAddr::V4(ip) = gateway.addr.ip() { + in_same_subnet(network.address, ip, network.net_mask) + } else { + false + } }) })?; @@ -106,7 +110,7 @@ impl IgdClient { match self.gateway.add_port( igd::PortMappingProtocol::TCP, addr.port(), - SocketAddrV4::new(self.state.address, addr.port()), + SocketAddrV4::new(self.state.address, addr.port()).into(), 0, // forever "p2p", ) { @@ -152,7 +156,7 @@ impl IgdClient { let _ignore = self.gateway.add_port( igd::PortMappingProtocol::TCP, addr.port(), - SocketAddrV4::new(self.state.address, addr.port()), + SocketAddrV4::new(self.state.address, addr.port()).into(), 60, // 60s "p2p", );