Skip to content
Snippets Groups Projects
Verified Commit 5abf278b authored by Yaroslav Dynnikov's avatar Yaroslav Dynnikov
Browse files

refactor: from / into types conversion

1. Trait `From` for `traft::row::Entry` now accepts a value instead of a
   reference in order to avoid unnecessary clones. A caller makes a
   clone itself if necessary.

2. Trait `From` implementation for `traft::Message` now accepts a
   reference instead of a value. Anyway, it uses msgpack under the hood
   and doesn't affect performance.
parent 9e743ff6
No related branches found
No related tags found
1 merge request!26refactor: from / into types conversion
Pipeline #3470 passed
......@@ -150,7 +150,7 @@ fn raft_propose(msg: Message) {
let raft_ref = stash.raft_node();
let raft_node = raft_ref.as_ref().expect("Picodata not running yet");
tlog!(Debug, "propose {:?} ................................", msg);
raft_node.propose(msg.into());
raft_node.propose(&msg);
tlog!(Debug, ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
}
......
......@@ -27,8 +27,8 @@ impl TryFrom<&[u8]> for Message {
}
}
impl From<Message> for Vec<u8> {
fn from(msg: Message) -> Vec<u8> {
impl From<&Message> for Vec<u8> {
fn from(msg: &Message) -> Vec<u8> {
if matches!(msg, Message::Empty) {
return Vec::new();
}
......@@ -82,7 +82,7 @@ inventory::submit!(crate::InnerTest {
let msg = Message::EvalLua {
code: "os.exit()".to_owned(),
};
let buf: Vec<u8> = msg.clone().into();
let buf: Vec<u8> = Vec::from(&msg);
assert_eq!(
("eval_lua", "os.exit()"),
rmp_serde::from_read_ref(&buf).unwrap()
......
......@@ -63,12 +63,14 @@ impl Node {
})
}
pub fn propose(&self, data: Vec<u8>) {
self.inbox.send(Request::Propose(data)).unwrap();
pub fn propose<T: Into<Vec<u8>>>(&self, data: T) {
let req = Request::Propose(data.into());
self.inbox.send(req).unwrap();
}
pub fn step(&self, msg: raft::Message) {
self.inbox.send(Request::Step(msg)).unwrap();
let req = Request::Step(msg);
self.inbox.send(req).unwrap();
}
}
......
......@@ -2,7 +2,6 @@ use ::raft::prelude as raft;
use serde::Deserialize;
use serde::Serialize;
use std::convert::TryFrom;
use std::convert::TryInto;
use crate::Message;
......@@ -15,15 +14,15 @@ pub struct Entry {
}
impl ::tarantool::tuple::AsTuple for Entry {}
impl TryFrom<&raft::Entry> for self::Entry {
impl TryFrom<raft::Entry> for self::Entry {
type Error = rmp_serde::decode::Error;
fn try_from(e: &raft::Entry) -> Result<Self, Self::Error> {
fn try_from(e: raft::Entry) -> Result<Self, Self::Error> {
Ok(Self {
entry_type: format!("{:?}", e.get_entry_type()),
index: e.get_index(),
term: e.get_term(),
msg: e.get_data().try_into()?,
msg: Message::try_from(e.get_data())?,
})
}
}
......@@ -40,7 +39,7 @@ impl From<self::Entry> for raft::Entry {
ret.set_entry_type(entry_type);
ret.set_index(row.index);
ret.set_term(row.term);
let bytes: Vec<u8> = Vec::from(row.msg);
let bytes: Vec<u8> = Vec::from(&row.msg);
ret.set_data(bytes.into());
ret
......@@ -49,7 +48,7 @@ impl From<self::Entry> for raft::Entry {
impl Default for Entry {
fn default() -> Self {
Self::try_from(&raft::Entry::default()).unwrap()
Self::try_from(raft::Entry::default()).expect("that's a bug")
}
}
......
......@@ -131,7 +131,7 @@ impl Storage {
if row.index >= high {
break;
}
ret.push(row.into());
ret.push(raft::Entry::from(row));
}
ret
......@@ -139,8 +139,8 @@ impl Storage {
pub fn persist_entries(entries: &Vec<raft::Entry>) {
let mut space = Space::find(SPACE_RAFT_LOG).unwrap();
for entry in entries {
let row = row::Entry::try_from(entry).unwrap();
for e in entries {
let row = row::Entry::try_from(e.clone()).unwrap();
space.insert(&row).unwrap();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment