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

refactor: use &GlobalConfig to avoid clone #217

Merged
merged 1 commit into from
Nov 7, 2023
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
8 changes: 4 additions & 4 deletions src/client/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ macro_rules! register_client {
impl $client {
pub const NAME: &str = $name;

pub fn init(global_config: $crate::config::GlobalConfig) -> Option<Box<dyn Client>> {
pub fn init(global_config: &$crate::config::GlobalConfig) -> Option<Box<dyn Client>> {
let model = global_config.read().model.clone();
let config = {
if let ClientConfig::$config(c) = &global_config.read().clients[model.client_index] {
Expand All @@ -62,7 +62,7 @@ macro_rules! register_client {
}
};
Some(Box::new(Self {
global_config,
global_config: global_config.clone(),
config,
model,
}))
Expand All @@ -75,9 +75,9 @@ macro_rules! register_client {

)+

pub fn init_client(config: $crate::config::GlobalConfig) -> anyhow::Result<Box<dyn Client>> {
pub fn init_client(config: &$crate::config::GlobalConfig) -> anyhow::Result<Box<dyn Client>> {
None
$(.or_else(|| $client::init(config.clone())))+
$(.or_else(|| $client::init(config)))+
.ok_or_else(|| {
let model = config.read().model.clone();
anyhow::anyhow!(
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ fn main() -> Result<()> {
exit(0);
}
let no_stream = cli.no_stream;
let client = init_client(config.clone())?;
let client = init_client(&config)?;
if stdin().is_terminal() {
match text {
Some(text) => start_directive(client.as_ref(), &config, &text, no_stream),
None => start_interactive(config),
None => start_interactive(&config),
}
} else {
let mut input = String::new();
Expand Down Expand Up @@ -128,8 +128,8 @@ fn start_directive(
config.write().save_message(input, &output)
}

fn start_interactive(config: GlobalConfig) -> Result<()> {
fn start_interactive(config: &GlobalConfig) -> Result<()> {
cl100k_base_singleton();
let mut repl: Repl = Repl::init(config.clone())?;
let mut repl: Repl = Repl::init(config)?;
repl.run()
}
10 changes: 5 additions & 5 deletions src/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ pub struct Repl {
}

impl Repl {
pub fn init(config: GlobalConfig) -> Result<Self> {
let editor = Self::create_editor(&config)?;
pub fn init(config: &GlobalConfig) -> Result<Self> {
let editor = Self::create_editor(config)?;

let prompt = ReplPrompt::new(config.clone());
let prompt = ReplPrompt::new(config);

let abort = create_abort_signal();

let clipboard = Clipboard::new().map(RefCell::new);

Ok(Self {
config,
config: config.clone(),
editor,
prompt,
clipboard,
Expand Down Expand Up @@ -228,7 +228,7 @@ impl Repl {
}
self.config.read().maybe_print_send_tokens(input);
let wg = WaitGroup::new();
let client = init_client(self.config.clone())?;
let client = init_client(&self.config)?;
let ret = render_stream(
input,
client.as_ref(),
Expand Down
6 changes: 4 additions & 2 deletions src/repl/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ pub struct ReplPrompt {
}

impl ReplPrompt {
pub fn new(config: GlobalConfig) -> Self {
Self { config }
pub fn new(config: &GlobalConfig) -> Self {
Self {
config: config.clone(),
}
}
}

Expand Down