Skip to content

Commit 1041d5f

Browse files
authored
chore: use Self where applicable (#1964)
1 parent 85cf58a commit 1041d5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+868
-868
lines changed

crates/shadowsocks-service/src/acl/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ impl Rules {
9191
rule_regex: RegexSet,
9292
rule_set: HashSet<String>,
9393
rule_tree: SubDomainsTree,
94-
) -> Rules {
94+
) -> Self {
9595
// Optimization, merging networks
9696
ipv4.simplify();
9797
ipv6.simplify();
9898

99-
Rules {
99+
Self {
100100
ipv4,
101101
ipv6,
102102
rule_regex,
@@ -167,7 +167,7 @@ struct ParsingRules {
167167

168168
impl ParsingRules {
169169
fn new(name: &'static str) -> Self {
170-
ParsingRules {
170+
Self {
171171
name,
172172
ipv4: IpRange::new(),
173173
ipv6: IpRange::new(),
@@ -342,7 +342,7 @@ pub struct AccessControl {
342342

343343
impl AccessControl {
344344
/// Load ACL rules from a file
345-
pub fn load_from_file<P: AsRef<Path>>(p: P) -> io::Result<AccessControl> {
345+
pub fn load_from_file<P: AsRef<Path>>(p: P) -> io::Result<Self> {
346346
trace!("ACL loading from {:?}", p.as_ref());
347347

348348
let file_path_ref = p.as_ref();
@@ -436,7 +436,7 @@ impl AccessControl {
436436
}
437437
}
438438

439-
Ok(AccessControl {
439+
Ok(Self {
440440
outbound_block: outbound_block.into_rules()?,
441441
black_list: bypass.into_rules()?,
442442
white_list: proxy.into_rules()?,

crates/shadowsocks-service/src/acl/sub_domains_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct DomainPart {
1111

1212
impl DomainPart {
1313
fn new() -> Self {
14-
DomainPart {
14+
Self {
1515
included: false,
1616
children: HashMap::new(),
1717
}
@@ -29,7 +29,7 @@ impl Debug for SubDomainsTree {
2929

3030
impl SubDomainsTree {
3131
pub fn new() -> Self {
32-
SubDomainsTree(HashMap::new())
32+
Self(HashMap::new())
3333
}
3434

3535
pub fn insert(&mut self, value: &str) {

crates/shadowsocks-service/src/config.rs

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -444,23 +444,23 @@ pub enum ConfigType {
444444
impl ConfigType {
445445
/// Check if it is local server type
446446
pub fn is_local(self) -> bool {
447-
self == ConfigType::Local
447+
self == Self::Local
448448
}
449449

450450
/// Check if it is remote server type
451451
pub fn is_server(self) -> bool {
452-
self == ConfigType::Server
452+
self == Self::Server
453453
}
454454

455455
/// Check if it is manager server type
456456
pub fn is_manager(self) -> bool {
457-
self == ConfigType::Manager
457+
self == Self::Manager
458458
}
459459

460460
/// Check if it is online config type (SIP008)
461461
#[cfg(feature = "local-online-config")]
462462
pub fn is_online_config(self) -> bool {
463-
self == ConfigType::OnlineConfig
463+
self == Self::OnlineConfig
464464
}
465465
}
466466

@@ -512,8 +512,8 @@ cfg_if! {
512512
cfg_if! {
513513
if #[cfg(any(target_os = "linux", target_os = "android"))] {
514514
/// Default TCP transparent proxy solution on this platform
515-
pub const fn tcp_default() -> RedirType {
516-
RedirType::Redirect
515+
pub const fn tcp_default() -> Self {
516+
Self::Redirect
517517
}
518518

519519
/// Available TCP transparent proxy types
@@ -524,8 +524,8 @@ cfg_if! {
524524
}
525525

526526
/// Default UDP transparent proxy solution on this platform
527-
pub const fn udp_default() -> RedirType {
528-
RedirType::TProxy
527+
pub const fn udp_default() -> Self {
528+
Self::TProxy
529529
}
530530

531531
/// Available UDP transparent proxy types
@@ -635,20 +635,20 @@ cfg_if! {
635635

636636
/// Check if transparent proxy is supported on this platform
637637
pub fn is_supported(self) -> bool {
638-
self != RedirType::NotSupported
638+
self != Self::NotSupported
639639
}
640640

641641
/// Name of redirect type (transparent proxy type)
642642
pub const fn name(self) -> &'static str {
643643
match self {
644644
// Dummy, shouldn't be used in any useful situations
645-
RedirType::NotSupported => "not_supported",
645+
Self::NotSupported => "not_supported",
646646

647647
#[cfg(any(target_os = "linux", target_os = "android"))]
648-
RedirType::Redirect => "redirect",
648+
Self::Redirect => "redirect",
649649

650650
#[cfg(any(target_os = "linux", target_os = "android"))]
651-
RedirType::TProxy => "tproxy",
651+
Self::TProxy => "tproxy",
652652

653653
#[cfg(any(
654654
target_os = "freebsd",
@@ -683,13 +683,13 @@ cfg_if! {
683683
impl FromStr for RedirType {
684684
type Err = InvalidRedirType;
685685

686-
fn from_str(s: &str) -> Result<RedirType, InvalidRedirType> {
686+
fn from_str(s: &str) -> Result<Self, InvalidRedirType> {
687687
match s {
688688
#[cfg(any(target_os = "linux", target_os = "android"))]
689-
"redirect" => Ok(RedirType::Redirect),
689+
"redirect" => Ok(Self::Redirect),
690690

691691
#[cfg(any(target_os = "linux", target_os = "android"))]
692-
"tproxy" => Ok(RedirType::TProxy),
692+
"tproxy" => Ok(Self::TProxy),
693693

694694
#[cfg(any(
695695
target_os = "freebsd",
@@ -725,8 +725,8 @@ pub enum ManagerServerHost {
725725
}
726726

727727
impl Default for ManagerServerHost {
728-
fn default() -> ManagerServerHost {
729-
ManagerServerHost::Ip(Ipv4Addr::UNSPECIFIED.into())
728+
fn default() -> Self {
729+
Self::Ip(Ipv4Addr::UNSPECIFIED.into())
730730
}
731731
}
732732

@@ -735,8 +735,8 @@ impl FromStr for ManagerServerHost {
735735

736736
fn from_str(s: &str) -> Result<Self, Self::Err> {
737737
match s.parse::<IpAddr>() {
738-
Ok(s) => Ok(ManagerServerHost::Ip(s)),
739-
Err(..) => Ok(ManagerServerHost::Domain(s.to_owned())),
738+
Ok(s) => Ok(Self::Ip(s)),
739+
Err(..) => Ok(Self::Domain(s.to_owned())),
740740
}
741741
}
742742
}
@@ -766,11 +766,11 @@ impl Display for ManagerServerModeError {
766766
impl FromStr for ManagerServerMode {
767767
type Err = ManagerServerModeError;
768768

769-
fn from_str(s: &str) -> Result<ManagerServerMode, Self::Err> {
769+
fn from_str(s: &str) -> Result<Self, Self::Err> {
770770
match s {
771-
"builtin" => Ok(ManagerServerMode::Builtin),
771+
"builtin" => Ok(Self::Builtin),
772772
#[cfg(unix)]
773-
"standalone" => Ok(ManagerServerMode::Standalone),
773+
"standalone" => Ok(Self::Standalone),
774774
_ => Err(ManagerServerModeError),
775775
}
776776
}
@@ -779,9 +779,9 @@ impl FromStr for ManagerServerMode {
779779
impl Display for ManagerServerMode {
780780
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
781781
match *self {
782-
ManagerServerMode::Builtin => f.write_str("builtin"),
782+
Self::Builtin => f.write_str("builtin"),
783783
#[cfg(unix)]
784-
ManagerServerMode::Standalone => f.write_str("standalone"),
784+
Self::Standalone => f.write_str("standalone"),
785785
}
786786
}
787787
}
@@ -815,8 +815,8 @@ pub struct ManagerConfig {
815815

816816
impl ManagerConfig {
817817
/// Create a ManagerConfig with default options
818-
pub fn new(addr: ManagerAddr) -> ManagerConfig {
819-
ManagerConfig {
818+
pub fn new(addr: ManagerAddr) -> Self {
819+
Self {
820820
addr,
821821
method: None,
822822
plugin: None,
@@ -858,19 +858,19 @@ impl ProtocolType {
858858
/// As string representation
859859
pub fn as_str(&self) -> &'static str {
860860
match *self {
861-
ProtocolType::Socks => "socks",
861+
Self::Socks => "socks",
862862
#[cfg(feature = "local-http")]
863-
ProtocolType::Http => "http",
863+
Self::Http => "http",
864864
#[cfg(feature = "local-tunnel")]
865-
ProtocolType::Tunnel => "tunnel",
865+
Self::Tunnel => "tunnel",
866866
#[cfg(feature = "local-redir")]
867-
ProtocolType::Redir => "redir",
867+
Self::Redir => "redir",
868868
#[cfg(feature = "local-dns")]
869-
ProtocolType::Dns => "dns",
869+
Self::Dns => "dns",
870870
#[cfg(feature = "local-tun")]
871-
ProtocolType::Tun => "tun",
871+
Self::Tun => "tun",
872872
#[cfg(feature = "local-fake-dns")]
873-
ProtocolType::FakeDns => "fake-dns",
873+
Self::FakeDns => "fake-dns",
874874
}
875875
}
876876

@@ -909,19 +909,19 @@ impl FromStr for ProtocolType {
909909

910910
fn from_str(s: &str) -> Result<Self, Self::Err> {
911911
match s {
912-
"socks" => Ok(ProtocolType::Socks),
912+
"socks" => Ok(Self::Socks),
913913
#[cfg(feature = "local-http")]
914-
"http" => Ok(ProtocolType::Http),
914+
"http" => Ok(Self::Http),
915915
#[cfg(feature = "local-tunnel")]
916-
"tunnel" => Ok(ProtocolType::Tunnel),
916+
"tunnel" => Ok(Self::Tunnel),
917917
#[cfg(feature = "local-redir")]
918-
"redir" => Ok(ProtocolType::Redir),
918+
"redir" => Ok(Self::Redir),
919919
#[cfg(feature = "local-dns")]
920-
"dns" => Ok(ProtocolType::Dns),
920+
"dns" => Ok(Self::Dns),
921921
#[cfg(feature = "local-tun")]
922-
"tun" => Ok(ProtocolType::Tun),
922+
"tun" => Ok(Self::Tun),
923923
#[cfg(feature = "local-fake-dns")]
924-
"fake-dns" => Ok(ProtocolType::FakeDns),
924+
"fake-dns" => Ok(Self::FakeDns),
925925
_ => Err(ProtocolTypeError),
926926
}
927927
}
@@ -1054,7 +1054,7 @@ pub struct LocalConfig {
10541054

10551055
impl LocalConfig {
10561056
/// Create a new `LocalConfig`
1057-
pub fn new(protocol: ProtocolType) -> LocalConfig {
1057+
pub fn new(protocol: ProtocolType) -> Self {
10581058
// DNS server runs in `TcpAndUdp` mode by default to maintain backwards compatibility
10591059
// see https://github.com/shadowsocks/shadowsocks-rust/issues/1281
10601060
let mode = match protocol {
@@ -1063,7 +1063,7 @@ impl LocalConfig {
10631063
_ => Mode::TcpOnly,
10641064
};
10651065

1066-
LocalConfig {
1066+
Self {
10671067
addr: None,
10681068

10691069
protocol,
@@ -1120,8 +1120,8 @@ impl LocalConfig {
11201120
}
11211121

11221122
/// Create a new `LocalConfig` with listen address
1123-
pub fn new_with_addr(addr: ServerAddr, protocol: ProtocolType) -> LocalConfig {
1124-
let mut config = LocalConfig::new(protocol);
1123+
pub fn new_with_addr(addr: ServerAddr, protocol: ProtocolType) -> Self {
1124+
let mut config = Self::new(protocol);
11251125
config.addr = Some(addr);
11261126
config
11271127
}
@@ -1260,8 +1260,8 @@ pub struct ServerInstanceConfig {
12601260

12611261
impl ServerInstanceConfig {
12621262
/// Create with `ServerConfig`
1263-
pub fn with_server_config(config: ServerConfig) -> ServerInstanceConfig {
1264-
ServerInstanceConfig {
1263+
pub fn with_server_config(config: ServerConfig) -> Self {
1264+
Self {
12651265
config,
12661266
acl: None,
12671267
#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -1286,8 +1286,8 @@ pub struct LocalInstanceConfig {
12861286

12871287
impl LocalInstanceConfig {
12881288
/// Create with `LocalConfig`
1289-
pub fn with_local_config(config: LocalConfig) -> LocalInstanceConfig {
1290-
LocalInstanceConfig { config, acl: None }
1289+
pub fn with_local_config(config: LocalConfig) -> Self {
1290+
Self { config, acl: None }
12911291
}
12921292
}
12931293

@@ -1435,8 +1435,8 @@ pub struct Error {
14351435
}
14361436

14371437
impl Error {
1438-
pub fn new(kind: ErrorKind, desc: &'static str, detail: Option<String>) -> Error {
1439-
Error { kind, desc, detail }
1438+
pub fn new(kind: ErrorKind, desc: &'static str, detail: Option<String>) -> Self {
1439+
Self { kind, desc, detail }
14401440
}
14411441
}
14421442

@@ -1479,8 +1479,8 @@ impl Display for Error {
14791479

14801480
impl Config {
14811481
/// Creates an empty configuration
1482-
pub fn new(config_type: ConfigType) -> Config {
1483-
Config {
1482+
pub fn new(config_type: ConfigType) -> Self {
1483+
Self {
14841484
server: Vec::new(),
14851485
local: Vec::new(),
14861486

@@ -1536,8 +1536,8 @@ impl Config {
15361536
}
15371537
}
15381538

1539-
fn load_from_ssconfig(config: SSConfig, config_type: ConfigType) -> Result<Config, Error> {
1540-
let mut nconfig = Config::new(config_type);
1539+
fn load_from_ssconfig(config: SSConfig, config_type: ConfigType) -> Result<Self, Error> {
1540+
let mut nconfig = Self::new(config_type);
15411541

15421542
// Client
15431543
//
@@ -2495,11 +2495,11 @@ impl Config {
24952495

24962496
impl DnsProtocol {
24972497
fn enable_tcp(&self) -> bool {
2498-
matches!(*self, DnsProtocol::Tcp | DnsProtocol::Both)
2498+
matches!(*self, Self::Tcp | Self::Both)
24992499
}
25002500

25012501
fn enable_udp(&self) -> bool {
2502-
matches!(*self, DnsProtocol::Udp | DnsProtocol::Both)
2502+
matches!(*self, Self::Udp | Self::Both)
25032503
}
25042504
}
25052505

@@ -2563,20 +2563,20 @@ impl Config {
25632563
}
25642564

25652565
/// Load Config from a `str`
2566-
pub fn load_from_str(s: &str, config_type: ConfigType) -> Result<Config, Error> {
2566+
pub fn load_from_str(s: &str, config_type: ConfigType) -> Result<Self, Error> {
25672567
let c = json5::from_str::<SSConfig>(s)?;
2568-
Config::load_from_ssconfig(c, config_type)
2568+
Self::load_from_ssconfig(c, config_type)
25692569
}
25702570

25712571
/// Load Config from a File
2572-
pub fn load_from_file<P: AsRef<Path>>(filename: P, config_type: ConfigType) -> Result<Config, Error> {
2572+
pub fn load_from_file<P: AsRef<Path>>(filename: P, config_type: ConfigType) -> Result<Self, Error> {
25732573
let filename = filename.as_ref();
25742574

25752575
let mut reader = OpenOptions::new().read(true).open(filename)?;
25762576
let mut content = String::new();
25772577
reader.read_to_string(&mut content)?;
25782578

2579-
let mut config = Config::load_from_str(&content[..], config_type)?;
2579+
let mut config = Self::load_from_str(&content[..], config_type)?;
25802580

25812581
// Record the path of the configuration for auto-reloading
25822582
config.config_path = Some(filename.to_owned());

0 commit comments

Comments
 (0)