Skip to content

Commit ce6b1a0

Browse files
committed
Chore: refactoring to not reinvent the wheel—or Cow as it were
1 parent ca3386c commit ce6b1a0

File tree

3 files changed

+13
-37
lines changed

3 files changed

+13
-37
lines changed

neo4j/src/driver/io/bolt/bolt_handler/hello_5x0.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::borrow::Cow;
1516
use std::collections::HashMap;
1617
use std::io::{Read, Write};
1718
use std::mem;
@@ -34,7 +35,6 @@ use super::super::{
3435
};
3536
use super::common::{check_no_notification_filter, write_auth_entries, write_str_entry};
3637
use crate::error_::Result;
37-
use crate::util::RefContainer;
3838
use crate::value::{ValueReceive, ValueSend};
3939

4040
pub(super) const SERVER_AGENT_KEY: &str = "server";
@@ -184,14 +184,14 @@ impl HelloHandler5x0 {
184184

185185
pub(super) fn extract_connection_hints(
186186
meta: &BoltMeta,
187-
) -> RefContainer<'_, HashMap<String, ValueReceive>> {
187+
) -> Cow<'_, HashMap<String, ValueReceive>> {
188188
match meta.get(HINTS_KEY) {
189-
Some(ValueReceive::Map(hints)) => RefContainer::Borrowed(hints),
189+
Some(ValueReceive::Map(hints)) => Cow::Borrowed(hints),
190190
Some(value) => {
191191
warn!("Server sent unexpected {HINTS_KEY} type {:?}", value);
192-
RefContainer::Owned(HashMap::new())
192+
Cow::Owned(HashMap::new())
193193
}
194-
None => RefContainer::Owned(HashMap::new()),
194+
None => Cow::Owned(HashMap::new()),
195195
}
196196
}
197197

neo4j/src/driver/io/pool/single_pool.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use log::{info, log_enabled, Level};
15+
use std::borrow::Cow;
1616
use std::collections::{HashSet, VecDeque};
1717
use std::ops::{Deref, DerefMut};
1818
use std::sync::Arc;
1919
use std::time::Duration;
2020

21+
use log::{info, log_enabled, Level};
2122
use parking_lot::lock_api::MutexGuard;
2223
use parking_lot::{Condvar, Mutex, RawMutex};
2324

@@ -30,7 +31,6 @@ use crate::driver::config::auth::{auth_managers, AuthToken};
3031
use crate::driver::config::AuthConfig;
3132
use crate::error_::{Neo4jError, Result};
3233
use crate::time::Instant;
33-
use crate::util::RefContainer;
3434

3535
type PoolElement = TcpBolt;
3636

@@ -95,13 +95,13 @@ impl InnerPool {
9595
) -> Result<PoolElement> {
9696
let auth = match session_auth {
9797
SessionAuth::None => match &self.config.auth {
98-
AuthConfig::Static(auth) => RefContainer::Borrowed(auth),
98+
AuthConfig::Static(auth) => Cow::Borrowed(auth),
9999
AuthConfig::Manager(manager) => {
100-
RefContainer::Owned(auth_managers::get_auth(manager.as_ref())?)
100+
Cow::Owned(auth_managers::get_auth(manager.as_ref())?)
101101
}
102102
},
103-
SessionAuth::Reauth(auth) => RefContainer::Borrowed(auth),
104-
SessionAuth::Forced(auth) => RefContainer::Borrowed(auth),
103+
SessionAuth::Reauth(auth) => Cow::Borrowed(auth),
104+
SessionAuth::Forced(auth) => Cow::Borrowed(auth),
105105
};
106106

107107
let address = Arc::clone(&self.address);
@@ -386,9 +386,9 @@ impl UnpreparedSinglePooledBolt {
386386
match session_auth {
387387
SessionAuth::None => {
388388
let new_auth = match &self.pool.config.auth {
389-
AuthConfig::Static(auth) => RefContainer::Borrowed(auth),
389+
AuthConfig::Static(auth) => Cow::Borrowed(auth),
390390
AuthConfig::Manager(manager) => {
391-
RefContainer::Owned(auth_managers::get_auth(manager.as_ref())?)
391+
Cow::Owned(auth_managers::get_auth(manager.as_ref())?)
392392
}
393393
};
394394
let reauth_params = ReauthParameters::new(new_auth.as_ref(), false);

neo4j/src/util.rs

-24
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::ops::Deref;
16-
1715
pub(crate) fn truncate_string(string: &str, start: usize, end: usize) -> &str {
1816
let mut chars = string.chars();
1917
for _ in 0..start {
@@ -29,28 +27,6 @@ pub(crate) fn truncate_string(string: &str, start: usize, end: usize) -> &str {
2927
chars.as_str()
3028
}
3129

32-
pub(crate) enum RefContainer<'a, T> {
33-
Owned(T),
34-
Borrowed(&'a T),
35-
}
36-
37-
impl<T> AsRef<T> for RefContainer<'_, T> {
38-
fn as_ref(&self) -> &T {
39-
match self {
40-
RefContainer::Owned(t) => t,
41-
RefContainer::Borrowed(t) => t,
42-
}
43-
}
44-
}
45-
46-
impl<T> Deref for RefContainer<'_, T> {
47-
type Target = T;
48-
49-
fn deref(&self) -> &Self::Target {
50-
self.as_ref()
51-
}
52-
}
53-
5430
macro_rules! concat_str {
5531
($a:expr, $b:expr) => {{
5632
const A: &str = $a;

0 commit comments

Comments
 (0)