Weird behavior using Vec

Open
#15 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
rust
Domain
cli

Research direction

Start with the provided minimal example and trace the confy::load and confy::store entry points while reproducing the Vec persistence behavior. Compare the loaded default values with the TOML output; done means removing items persists correctly and the distinct default items are not duplicated.

Written by the indexing model from the issue text.

Description

bug

I'm using confy 0.3.1, and it seems that I am unable to remove items from a vec in my config (adding items works fine). But when I was trying to build a simple example to demonstrate the issue, I found a very confusing one.

Here's my code:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct MyItem {
    name: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct MyConfig {
    items: Vec<MyItem>,
}

/// `MyConfig` implements `Default`
impl ::std::default::Default for MyConfig {
    fn default() -> Self {
        Self {
            items: vec![MyItem { name: "foo".into() }, MyItem { name: "bar".into() }],
        }
    }
}

fn main() -> Result<(), ::std::io::Error> {
    let mut cfg: MyConfig = confy::load("foobar")?;
    println!("{:?}", cfg);
    cfg.items.remove(0);
    println!("{:?}", cfg);
    confy::store("foobar", cfg)?;
    Ok(())
}

This is what I get in the config file after running it (any number of times):

[[items]]
name = 'bar'

[[items]]
name = 'bar'

I expected one item after the first run and zero after subsequent runs. And the default vec has two different items (foo and bar) whereas the toml file have the same item twice.

Dominant language
Rust
Stars
1k
Forks
78
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-cli/confy

All issues in rust-cli/confy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.