Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: refactoring to not reinvent the wheel—or Cow as it were #31

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions neo4j/src/driver/io/bolt/bolt_handler/hello_5x0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;
use std::collections::HashMap;
use std::io::{Read, Write};
use std::mem;
Expand All @@ -34,7 +35,6 @@ use super::super::{
};
use super::common::{check_no_notification_filter, write_auth_entries, write_str_entry};
use crate::error_::Result;
use crate::util::RefContainer;
use crate::value::{ValueReceive, ValueSend};

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

pub(super) fn extract_connection_hints(
meta: &BoltMeta,
) -> RefContainer<'_, HashMap<String, ValueReceive>> {
) -> Cow<'_, HashMap<String, ValueReceive>> {
match meta.get(HINTS_KEY) {
Some(ValueReceive::Map(hints)) => RefContainer::Borrowed(hints),
Some(ValueReceive::Map(hints)) => Cow::Borrowed(hints),
Some(value) => {
warn!("Server sent unexpected {HINTS_KEY} type {:?}", value);
RefContainer::Owned(HashMap::new())
Cow::Owned(HashMap::new())
}
None => RefContainer::Owned(HashMap::new()),
None => Cow::Owned(HashMap::new()),
}
}

Expand Down
16 changes: 8 additions & 8 deletions neo4j/src/driver/io/pool/single_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use log::{info, log_enabled, Level};
use std::borrow::Cow;
use std::collections::{HashSet, VecDeque};
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use std::time::Duration;

use log::{info, log_enabled, Level};
use parking_lot::lock_api::MutexGuard;
use parking_lot::{Condvar, Mutex, RawMutex};

Expand All @@ -30,7 +31,6 @@ use crate::driver::config::auth::{auth_managers, AuthToken};
use crate::driver::config::AuthConfig;
use crate::error_::{Neo4jError, Result};
use crate::time::Instant;
use crate::util::RefContainer;

type PoolElement = TcpBolt;

Expand Down Expand Up @@ -95,13 +95,13 @@ impl InnerPool {
) -> Result<PoolElement> {
let auth = match session_auth {
SessionAuth::None => match &self.config.auth {
AuthConfig::Static(auth) => RefContainer::Borrowed(auth),
AuthConfig::Static(auth) => Cow::Borrowed(auth),
AuthConfig::Manager(manager) => {
RefContainer::Owned(auth_managers::get_auth(manager.as_ref())?)
Cow::Owned(auth_managers::get_auth(manager.as_ref())?)
}
},
SessionAuth::Reauth(auth) => RefContainer::Borrowed(auth),
SessionAuth::Forced(auth) => RefContainer::Borrowed(auth),
SessionAuth::Reauth(auth) => Cow::Borrowed(auth),
SessionAuth::Forced(auth) => Cow::Borrowed(auth),
};

let address = Arc::clone(&self.address);
Expand Down Expand Up @@ -386,9 +386,9 @@ impl UnpreparedSinglePooledBolt {
match session_auth {
SessionAuth::None => {
let new_auth = match &self.pool.config.auth {
AuthConfig::Static(auth) => RefContainer::Borrowed(auth),
AuthConfig::Static(auth) => Cow::Borrowed(auth),
AuthConfig::Manager(manager) => {
RefContainer::Owned(auth_managers::get_auth(manager.as_ref())?)
Cow::Owned(auth_managers::get_auth(manager.as_ref())?)
}
};
let reauth_params = ReauthParameters::new(new_auth.as_ref(), false);
Expand Down
24 changes: 0 additions & 24 deletions neo4j/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops::Deref;

pub(crate) fn truncate_string(string: &str, start: usize, end: usize) -> &str {
let mut chars = string.chars();
for _ in 0..start {
Expand All @@ -29,28 +27,6 @@ pub(crate) fn truncate_string(string: &str, start: usize, end: usize) -> &str {
chars.as_str()
}

pub(crate) enum RefContainer<'a, T> {
Owned(T),
Borrowed(&'a T),
}

impl<T> AsRef<T> for RefContainer<'_, T> {
fn as_ref(&self) -> &T {
match self {
RefContainer::Owned(t) => t,
RefContainer::Borrowed(t) => t,
}
}
}

impl<T> Deref for RefContainer<'_, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
self.as_ref()
}
}

macro_rules! concat_str {
($a:expr, $b:expr) => {{
const A: &str = $a;
Expand Down