From 33bf1dc1b32db8942088b5b0b084f457208a2eec Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Tue, 2 Feb 2021 14:42:21 -0800 Subject: [PATCH] Test showing surprising inference of the unit type. --- .envrc | 1 + .gitignore | 1 + Cargo.lock | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 12 +++++++++ default.nix | 19 ++++++++++++++ src/main.rs | 51 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 default.nix create mode 100644 src/main.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4a4726a --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use_nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3babba7 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,71 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustinferencetest" +version = "0.1.0" +dependencies = [ + "serde", + "serde_derive", + "toml", +] + +[[package]] +name = "serde" +version = "1.0.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" + +[[package]] +name = "serde_derive" +version = "1.0.123" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..abcde13 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "rustinferencetest" +version = "0.1.0" +authors = ["Bill Thiede "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = "1" +serde_derive = "1" +toml = "0.5.8" diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..7c5ea65 --- /dev/null +++ b/default.nix @@ -0,0 +1,19 @@ +let + unstableTarball = fetchTarball + "https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz"; + pkgs = import { }; + unstable = import unstableTarball { }; + +in with pkgs; +pkgs.mkShell { + name = "rust"; + buildInputs = [ + openssl + pkg-config + unstable.cargo + unstable.rustc + unstable.rustfmt + unstable.rust-analyzer + postgresql + ]; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e384f37 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,51 @@ +use serde_derive::{Deserialize, Serialize}; +use std::collections::HashMap; + +#[derive(Debug, Serialize, Deserialize)] +struct Config { + sites: HashMap, +} +#[derive(Debug, Serialize, Deserialize)] +struct Site { + url: String, + title: String, + hover: Option, + main_image: String, + alternate_image: Option, +} + +fn main() -> Result<(), Box> { + let c = Config { + sites: vec![ + ( + "xkcd".to_string(), + Site { + url: "https://xkcd.com/1/".to_string(), + title: "title".to_string(), + hover: None, + main_image: "img".to_string(), + alternate_image: None, + }, + ), + ( + "sinfest".to_string(), + Site { + url: "https://sinfest.net/view.php?date=2000-01-17".to_string(), + title: "title".to_string(), + hover: None, + main_image: "img".to_string(), + alternate_image: None, + }, + ), + ] + .into_iter() + .collect(), + }; + let s = toml::to_string(&c)?; + println!("s {}", s); + // c is inferred as the unit type judging by the error you get when you print with Display + // formatting (i.e. "{}") instead of debug formating (i.e. "{:?}"). + let c = toml::from_str(&s)?; + println!("c {:?}", c); + Ok(()) +}