Skip to content

Commit

Permalink
[refactor] Passing by references instead of cloning
Browse files Browse the repository at this point in the history
Signed-off-by: dd di cesare <[email protected]>
  • Loading branch information
didierofrivia committed Sep 16, 2024
1 parent 2ca0010 commit 34b3b81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions src/operation_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ impl Operation {
}
}

pub fn get_state(&self) -> State {
self.state.clone()
pub fn get_state(&self) -> &State {
&self.state
}

pub fn get_result(&self) -> Result<u32, Status> {
self.result
}

pub fn get_extension_type(&self) -> ExtensionType {
self.extension.extension_type.clone()
pub fn get_extension_type(&self) -> &ExtensionType {
&self.extension.extension_type
}

pub fn get_failure_mode(&self) -> FailureMode {
self.extension.failure_mode.clone()
pub fn get_failure_mode(&self) -> &FailureMode {
&self.extension.failure_mode
}
}

Expand Down Expand Up @@ -260,24 +260,24 @@ mod tests {
fn operation_getters() {
let operation = build_operation();

assert_eq!(operation.get_state(), State::Pending);
assert_eq!(operation.get_extension_type(), ExtensionType::RateLimit);
assert_eq!(operation.get_failure_mode(), FailureMode::Deny);
assert_eq!(*operation.get_state(), State::Pending);
assert_eq!(*operation.get_extension_type(), ExtensionType::RateLimit);
assert_eq!(*operation.get_failure_mode(), FailureMode::Deny);
assert_eq!(operation.get_result(), Ok(0));
}

#[test]
fn operation_transition() {
let mut operation = build_operation();
assert_eq!(operation.result, Ok(0));
assert_eq!(operation.get_state(), State::Pending);
assert_eq!(*operation.get_state(), State::Pending);
let mut res = operation.trigger();
assert_eq!(res, Ok(200));
assert_eq!(operation.get_state(), State::Waiting);
assert_eq!(*operation.get_state(), State::Waiting);
res = operation.trigger();
assert_eq!(res, Ok(200));
assert_eq!(operation.result, Ok(200));
assert_eq!(operation.get_state(), State::Done);
assert_eq!(*operation.get_state(), State::Done);
}

#[test]
Expand Down Expand Up @@ -317,35 +317,35 @@ mod tests {

let mut op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().get_result(), Ok(200));
assert_eq!(op.unwrap().get_state(), State::Waiting);
assert_eq!(*op.unwrap().get_state(), State::Waiting);
assert_eq!(
operation_dispatcher.waiting_operations.borrow_mut().len(),
1
);

op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().get_result(), Ok(200));
assert_eq!(op.unwrap().get_state(), State::Done);
assert_eq!(*op.unwrap().get_state(), State::Done);

op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().get_result(), Ok(0));
assert_eq!(op.unwrap().get_state(), State::Pending);
assert_eq!(*op.unwrap().get_state(), State::Pending);
assert_eq!(
operation_dispatcher.waiting_operations.borrow_mut().len(),
0
);

op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().get_result(), Ok(200));
assert_eq!(op.unwrap().get_state(), State::Waiting);
assert_eq!(*op.unwrap().get_state(), State::Waiting);
assert_eq!(
operation_dispatcher.waiting_operations.borrow_mut().len(),
1
);

op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().get_result(), Ok(200));
assert_eq!(op.unwrap().get_state(), State::Done);
assert_eq!(*op.unwrap().get_state(), State::Done);

op = operation_dispatcher.next();
assert!(op.is_none());
Expand Down
2 changes: 1 addition & 1 deletion src/service/grpc_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Message for GrpcMessageResponse {

impl GrpcMessageResponse {
pub fn new(
extension_type: ExtensionType,
extension_type: &ExtensionType,
res_body_bytes: &Bytes,
status_code: u32,
) -> GrpcMessageResult<GrpcMessageResponse> {
Expand Down

0 comments on commit 34b3b81

Please sign in to comment.