These are chat archives for rust-lang/rust

26th
Jul 2018
Ichoran
@Ichoran
Jul 26 2018 00:05
Is there an efficient way to return an error if you try to add a map entry with a duplicate key? insert seems the most efficient, but that consumes the key so it's not available for reporting in the error message.
(Ironically, it actually doesn't consume the key in that case, but the return type is Option<V> not Option<(K, V)>.)
trsh
@trsh
Jul 26 2018 08:28
Can I somehave feed static str to an derive attribute?
#[schema = JSON_SCHEMA] // JSON_SCHEMA = static str declared above
struct XX;
Sylwester Rąpała
@xoac
Jul 26 2018 08:29
@Ichoran
I do this exactly like you described:
            let rm = elements.insert(key.to_string(), value);
            if let Some(rm) = rm {
                warn!("Element was replaced, removed entry: {:?}", rm);
            }
trsh
@trsh
Jul 26 2018 08:34
this #[schema = format!("\"{}\"", JSON_SCHEMA)] aint working :(
Lyle Mantooth
@IslandUsurper
Jul 26 2018 11:43
format! returns a String, not a &'static str
trsh
@trsh
Jul 26 2018 15:50
I have this json str wrapped in r#"{}"#
But then, actually somwhere in the middle i need to use # symbol
I tried to escape it with \#, but that says invalid escape
If I double \\#, it works, but the \ is comming with the string
Any ideas how to escape, and get rid of slash?
Lyle Mantooth
@IslandUsurper
Jul 26 2018 16:26
@trsh use r##"{}"##. Unless you think you need two #s, but then you just keep adding more. Or maybe you can use different characters there. The Reference knows.
Actually, since it's in quotes anyway, do you need to escape #?
According to how I read https://doc.rust-lang.org/stable/reference/tokens.html#raw-string-literals, you really only need ## when you have "# together.
trsh
@trsh
Jul 26 2018 16:39
@IslandUsurper Yeah, works
Ichoran
@Ichoran
Jul 26 2018 17:59
@xoac - Your key example wasn't what I was getting at, because you created a new key with to_string(). I was hoping to use the existing key. There are workarounds but I was hoping for something faster.
trsh
@trsh
Jul 26 2018 18:05
Hey! Is there some tool, that can Put license (top comment) in all project files? AS I just realized that, and do not want todo manually
Ichoran
@Ichoran
Jul 26 2018 19:56
Is there a way to know definitively (other than reading the source code) what visitors the default deserialize_any is going to call when parsing JSON? There's an example in the docs that suggests unit, i64, u64, str, seq, and map. But I know that's not right; f64 is called as well. On the other hand, the serde_json deserializer defines a huge pile of visitors, none of which (aside from those I listed above) I've been able to access by feeding in JSON.
Ichoran
@Ichoran
Jul 26 2018 20:03
It all just feels very type-unsafe, though I have to admit it was incredibly easy. I don't know if valid JSON is going to throw an error because I missed a visitor method.
Zakarum
@omni-viral
Jul 26 2018 20:37
@Ichoran Aren't Visitor must be implemented by user's type (possibly with derive(Deserilize))?
Visitor can expect any subset of possible types (for visit_* methods) and just return an error if unexpected type encountered
You can't know upfront what methods will be called by deserialize_any. You must assume it can call any of them
And you shouldn't use serde and limit it use for json.
Ichoran
@Ichoran
Jul 26 2018 20:47
@omni-viral - I'm sorry, but that isn't helpful advice. I need to catch and report cases where a JSON file has duplicate keys in maps, and otherwise the JSON may be arbitrary (not a serialization of a struct). serde_json silently ignores duplicate keys, which is not acceptable. I want to reuse as much code as possible, and I know for certain that it is an error to be called on anything but JSON.
So the question is: which visit_ methods are actually called during JSON deserialization by serde_json::from_whatever during a parse of valid JSON, if my custom deserializer just defers to deserialize_any? As far as I can tell, what I've got works, but I don't know how to tell if there's a case where it would fail.
This might seem kind of esoteric, but I don't see how one could write a custom JSON deserializer without knowing exactly that, so I thought it would be somewhere obvious. I haven't found it yet, though (or anyone who knows).
Zakarum
@omni-viral
Jul 26 2018 20:58
@Ichoran Maybe you need another crate for json then?
Cause what you need does't sound like job for serde
Ichoran
@Ichoran
Jul 26 2018 21:00
Do you have a suggestion?
Zakarum
@omni-viral
Jul 26 2018 21:01
There are plenty json crates. Starting with one named json
Ichoran
@Ichoran
Jul 26 2018 21:04
That one has Object as a BTreeMap, which means it doesn't support duplicate keys either.
Judson Lester
@nyarly
Jul 26 2018 23:33
I keep mentally tripping over https://hyper.rs/hyper/master/hyper/struct.HeaderMap.html where HeaderMap is defined pub struct HeaderMap<T = HeaderValue>. Doesn't that mean that T could simply be replaced with HeaderValue? Is there some consequence of using T = HeaderValue that I don't see?