id stringlengths 16 145 | text stringlengths 1 179k | title stringclasses 1
value |
|---|---|---|
r_std_hashmap/614907_20_0.txt | but then I have troubles of getting ownership of the str that is in the
TreeSet..
KillTheMule June 24, 2021, 3:02pm 6 | |
r_std_hashmap/36349_30_0.txt | Additional navigation options
* Code
* Issues
* Pull requests
* Discussions
* Actions
* Projects
* Security
* Insights | |
r_std_hashmap/enum.Type.html5_0_0.txt | Docs.rs
* oters-0.1.8 | |
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_2_0.txt | @ameobea10 ⢠Portfolio ⢠Contact ⢠Blog ⢠Professional
Experience
# Writing a Hashmap-to-Struct Procedural Macro in Rust | |
r_std_hashmap/enum.Type.html5_35_0.txt | source
#### pub fn sub_generic (&self, var: & String , t: & Type ) -> Type | |
r_std_hashmap/enum.Type.html5_39_0.txt | ### impl Clone for Type
source § | |
r_std_hashmap/r_std_hashmap_14_0.txt | 1.27.0 · [ source ](../../src/std/collections/hash/map.rs.html#1187-1193)
#### pub fn remove_entry <Q>(&mut self, k: [ &Q
](../primitive.reference.html) ) -> [ Option ](../option/enum.Option.html
"enum std::option::Option") < [ (K, V) ](../primitive.tuple.html) >
where K: [ Borrow ](../borrow/trait.Borrow.html "... | |
r_std_hashmap/enum.Type.html5_37_0.txt | source
#### pub fn is_stable (&self) -> Result < bool , Error > | |
r_std_hashmap/enum.Type.html5_43_0.txt | ### impl Debug for Type
source § | |
r_std_hashmap/enum.Type.html5_8_0.txt | * Clone
* Debug
* Display
* PartialEq<Type>
* StructuralPartialEq
### Auto Trait Implementations | |
r_std_hashmap/36349_88_0.txt | Mention
Reference | |
r_std_hashmap/enum.Type.html5_25_0.txt | ### FixVar( String )
§ | |
r_std_hashmap/614907_28_0.txt | Shared ownership is expressed via ` Arc ` type. In your case, it's `
Arc<String> ` or a non-growable slimmer version of ` Arc<str> ` . This is like
` &str ` , but the reference is non-temporary thanks to ` Arc ` .
You can also look for string interning crates that store one copy of a
string either globally or per s... | |
r_std_hashmap/614907_5_0.txt | One additional point is, those strings are immutable so I guess they shoud
actually be ` str ` instead of ` String ` ?
I've tried some for of implementation that (spoiler alert) doesn't work. What
I'm struggling with is I go to ` str ` then to ` &str ` then to ` String ` but
I'm not quite getting it what is best way t... | |
r_std_hashmap/614907_9_0.txt | agiuliano:
> those strings are immutable | |
r_std_hashmap/36349_65_0.txt | Comment options
* | |
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_4_0.txt | In one of my projects, I recently had the need to take a HashMap filled with
settings and configuration options and use it to populate the fields of a
struct. If a certain setting wasn't in the hashmap, then the default value for
the struct was used.
My first approach was to write a manual switch statement checking fo... | |
r_std_hashmap/36349_1_0.txt | Toggle navigation
Sign in | |
r_std_hashmap/36349_49_0.txt | Answered by adamreichold Dec 19, 2023
> You can see the "foreign" structs are: | |
r_std_hashmap/enum.Type.html5_88_0.txt | ### impl<T, U> TryInto <U> for T where U: TryFrom <T>,
§ | |
r_std_hashmap/614907_44_0.txt | agiuliano:
> Sorry about the dumb question but this ownership thing is driving me nuts | |
r_std_hashmap/36349_31_0.txt | # Sharing Rust structs in foreign libraries / Writing complex wrapper classes
#3634
Answered by adamreichold | |
r_std_hashmap/36349_66_0.txt | # {{title}}
Something went wrong. | |
r_std_hashmap/enum.Type.html5_61_0.txt | source §
### impl<T> Borrow <T> for T where T: ? Sized , | |
r_std_hashmap/r_std_hashmap_2_3.txt | \),+25\),%0A++++\(Viking::new\(%22Olaf%22,+%22Denmark%22\),+24\),%0A++++\(Viking::new\(%22Harald%22,+%22Iceland%22\),+12\),%0A%5D\);%0A%0A//+Use+derived+implementation+to+print+the+status+of+the+vikings.%0Afor+\(viking,+health\)+in+%26vikings+%7B%0A++++println!\(%22%7Bviking:?%7D+has+%7Bhealth%7D+hp%22\);%0A%7D%0A%7D&e... | |
r_std_hashmap/enum.Type.html5_1_0.txt | * oters 0.1.8
* Permalink
* Docs.rs crate page
* MIT
* Links
* Documentation
* Repository
* Crates.io
* Source | |
r_std_hashmap/enum.Type.html5_75_0.txt | #### type Owned = T
The resulting type after obtaining ownership. | |
r_std_hashmap/36349_59_0.txt | adamreichold
Dec 19, 2023 | |
r_std_hashmap/36349_45_0.txt | The issue arises when I attempt to implement something like this (ignore how
terrible the indirection is):
>>> from pyo3_complex_wrapper import Foo
>>> foo = Foo()
>>> bar = foo["greeting"][0]
>>> bar.word = "goodbye"
>>> print(foo.compute())
goodbyegoodbye
therethere
worldwor... | |
r_std_hashmap/614907_33_0.txt | 1 Like
agiuliano June 24, 2021, 7:02pm 10 | |
r_std_hashmap/614907_18_0.txt |
use std::collections::{BTreeSet, HashMap};
pub struct OrderedStrings<'a> {
ordered_symbols: Vec<&'a str>,
index_by_symbol: HashMap<&'a str, usize>,
}
impl OrderedStrings<'_> {
pub fn new(symbols: BTreeSet<&str>) -> OrderedStrings {
let mut index_by... | |
r_std_hashmap/r_std_hashmap_13_3.txt | V>>
ð¬ This is a nightly-only experimental API. ( ` map_try_insert ` [ #82766
](https://github.com/rust-lang/rust/issues/82766) )
Tries to insert a key-value pair into the map, and returns a mutable reference
to the value in the entry.
If the map already had this key present, nothing is updated, and an error
cont... | |
r_std_hashmap/614907_29_0.txt | 4 Likes
KillTheMule June 24, 2021, 5:35pm 9 | |
r_std_hashmap/r_std_hashmap_6_0.txt | 1.10.0 · [ source ](../../src/std/collections/hash/map.rs.html#456-458)
#### pub fn values_mut (&mut self) -> [ ValuesMut
](hash_map/struct.ValuesMut.html "struct
std::collections::hash_map::ValuesMut") <'_, K, V> â
An iterator visiting all values mutably in arbitrary order. The iterator
element type is ` &'a ... | |
r_std_hashmap/36349_50_0.txt | Those structures do not support shared ownership (not even in Rust) and hence
you cannot use that on language level. You would would have to introduce
something like ` Arc ` or our own ` Py ` smart pointer into the mix so that
you would store e.g. ` HashMap<String, Vec<Arc<Bar>> ` or ` HashMap<String,
Vec<Py<Bar>> ` ... | |
r_std_hashmap/r_std_hashmap_4_0.txt | 1.7.0 · [ source ](../../src/std/collections/hash/map.rs.html#314-316)
#### pub fn with_capacity_and_hasher (capacity: [ usize
](../primitive.usize.html) , hasher: S) -> [ HashMap
](hash_map/struct.HashMap.html "struct std::collections::hash_map::HashMap")
<K, V, S>
Creates an empty ` HashMap ` with at least the... | |
r_std_hashmap/614907_50_0.txt | agiuliano:
> bringing me to push stuff out | |
r_std_hashmap/614907_6_0.txt |
pub struct OrderedStrings {
ordered_symbols: Vec<str>,
index_by_symbol: HashMap<str, usize>,
}
impl OrderedStrings {
pub fn new(symbols: BTreeSet<&str>) -> OrderedStrings {
let mut index_by_symbol: HashMap<&str, usize> = HashMap::new();
let mut ... | |
r_std_hashmap/enum.Type.html5_91_0.txt | Performs the conversion.
§ | |
r_std_hashmap/36349_86_0.txt | Unordered list
Task list | |
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_1_0.txt | @ameobea10
cprimozic.net | |
r_std_hashmap/36349_93_0.txt | * Terms
* Privacy
* Security
* Status
* Docs
* Contact
* Manage cookies
* Do not share my personal information
You can’t perform that action at this time. | |
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_19_0.txt |
#[test]
fn simbroker_settings_hashmap_population() {
let mut hm = HashMap::new();
hm.insert(String::from("ping_ns"), String::from("2000"));
let settings = SimBrokerSettings::from_hashmap(hm);
assert_eq!(settings.ping_ns, 2000);
}
This creates a HashMap with one ent... | |
r_std_hashmap/36349_71_0.txt | All reactions
Comment options | |
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_5_0.txt |
let settings = SimbrokerSettings::default();
for (k, v) in hm.iter() {
match k {
"starting_balance" => {
settings.starting_balance = try!(parse_val(v));
},
"ping_ns" => {
settings.ping_ns_balance = try!(parse_val(v));
},
...
... | |
r_std_hashmap/36349_14_0.txt | Resources
* Learning Pathways
* White papers, Ebooks, Webinars
* Customer Stories
* Partners | |
r_std_hashmap/614907_26_0.txt | Maybe option 2 makes more sense?
kornel June 24, 2021, 4:30pm 8 | |
r_std_hashmap/614907_27_0.txt | agiuliano:
> Now what I'm bugged with is ownership.. since the vector and hashmap contain
> the exact same string, I'd like to store them only once in memory. | |
r_std_hashmap/36349_12_0.txt | For
* Enterprise
* Teams
* Startups
* Education | |
r_std_hashmap/r_std_hashmap_12_0.txt | 1.56.0 · [ source ](../../src/std/collections/hash/map.rs.html#831-833)
#### pub fn shrink_to (&mut self, min_capacity: [ usize
](../primitive.usize.html) )
Shrinks the capacity of the map with a lower limit. It will drop down no lower
than the supplied limit while maintaining the internal rules and possibly
lea... | |
r_std_hashmap/36349_2_0.txt | * Product
* Actions | |
r_std_hashmap/enum.Type.html5_16_0.txt | ### Float
§ | |
r_std_hashmap/36349_60_0.txt | Maintainer
\- | |
r_std_hashmap/enum.Type.html5_47_0.txt | Formats the value using the given formatter. Read more
source § | |
r_std_hashmap/36349_7_0.txt | Write better code with AI
* Code review | |
r_std_hashmap/enum.Type.html5_18_0.txt | ### Bool
§ | |
r_std_hashmap/r_std_hashmap_9_0.txt | 1.18.0 · [ source ](../../src/std/collections/hash/map.rs.html#688-693)
#### pub fn retain <F>(&mut self, f: F)
where F: [ FnMut ](../ops/trait.FnMut.html "trait std::ops::FnMut") ( [ &K
](../primitive.reference.html) , [ &mut V ](../primitive.reference.html) ) ->
[ bool ](../primitive.bool.html) ,
Retains only... | |
r_std_hashmap/614907_13_0.txt | erelde June 24, 2021, 12:09pm 4
You probably could learn a lot by using a reference counted smart pointer. You
could also use a map which keeps the insertion order for you like ` indexmap
` (or study its code) @bluss . playground with ref counting | |
r_std_hashmap/614907_23_0.txt | Thanks! yeah that's probably the main problem I agree: knowing when those
strings come from.
So what I'm trying to do is to have a more structured object that has other
info, one of these being those strings.
So for instance let's say we have some Posts (that are ordered by creation
date). Such posts have tags.
Fr... | |
r_std_hashmap/enum.Type.html5_49_0.txt | #### fn eq (&self, other: & Type ) -> bool
This method tests for ` self ` and ` other ` values to be equal, and is used
by ` == ` . | |
r_std_hashmap/36349_21_0.txt | Search syntax tips
# Provide feedback | |
r_std_hashmap/36349_79_0.txt | Category
❓ | |
r_std_hashmap/enum.Type.html5_59_0.txt | ### impl<T> Any for T where T: 'static + ? Sized ,
source § | |
r_std_hashmap/614907_45_0.txt | No worries But: I don't understand your question. Can you phrase it
differently? I'll try anyways:
agiuliano: | |
r_std_hashmap/enum.Type.html5_26_0.txt | ### Generic( Vec < String , Global >, Box < Type , Global >)
§ | |
r_std_hashmap/36349_13_0.txt | By Solution
* CI/CD & Automation
* DevOps
* DevSecOps | |
r_std_hashmap/36349_18_0.txt | * Topics
* Trending
* Collections
* Pricing | |
r_std_hashmap/36349_8_0.txt | Manage code changes
* Issues | |
r_std_hashmap/614907_34_0.txt | alright! thanks for your help! will try it out!
e: actually last question I have on this approach.. if I have to make a getter
for the vector, how can I then avoid to have a reference to a reference, i.e. | |
r_std_hashmap/writing-a-hashmap-to-struct-procedural-macro-in-rust8_13_0.txt | Most of this code is pretty simple; the complicated bit is with in ` #() `
block. Quote treats the ` #() ` block much like Rust's standard macros treat `
$() ` : they use it to repeat code. It took me a while to understand how that
repetition operator worked (it's quite different from traditional loops and
iterators), ... | |
r_std_hashmap/36349_40_0.txt | ##
warriorstar-orion | |
r_std_hashmap/r_std_hashmap_15_0.txt | 1.4.0 · [ source ](../../src/std/collections/hash/map.rs.html#3157-3177) §
### impl<'a, K, V, S> [ Extend ](../iter/trait.Extend.html "trait
std::iter::Extend") <( [ &'a K ](../primitive.reference.html) , [ &'a V
](../primitive.reference.html) )> for [ HashMap ](hash_map/struct.HashMap.html
"struct std::collection... | |
r_std_hashmap/enum.Type.html5_50_0.txt | 1.0.0 · source §
#### fn ne (&self, other: &Rhs ) -> bool | |
r_std_hashmap/614907_0_0.txt | The Rust Programming Language Forum
# String, str, and borrowing on strucs | |
r_std_hashmap/36349_20_0.txt | Search
Clear | |
r_std_hashmap/enum.Type.html5_4_0.txt | ☰
## | |
r_std_hashmap/36349_22_0.txt | We read every piece of feedback, and take your input very seriously.
Include my email address so I can be contacted | |
r_std_hashmap/enum.Type.html5_34_0.txt | source
#### pub fn from_enumdef ( id: String , params: Vec < String , Global
>, variants: Vec <( String , Option < Spanned < TypeExpr >>), Global
>, t_decs: &Dag< HashMap < String , Type , RandomState >, String ,
u32 > ) -> Result < Type , Spanned < Error >> | |
r_std_hashmap/614907_35_0.txt |
pub fn get_symbol(&self, index: usize) -> Option<&str> {
self.ordered_symbols.get(index) // this doesn't work as it return Option<&&string>
}
agiuliano June 24, 2021, 7:33pm 11 | |
r_std_hashmap/36349_73_0.txt | Something went wrong.
Quote reply | |
r_std_hashmap/36349_43_0.txt | and my wrapper pyclasses are
#[pyclass(module = "pyo3_complex_wrapper")]
#[pyo3(name = "Foo")]
pub struct FooWrapper(foreign::Foo);
#[pyclass(module = "pyo3_complex_wrapper")]
#[pyo3(name = "Bar")]
pub struct BarWrapper(foreign::Bar); | |
r_std_hashmap/enum.Type.html5_11_0.txt | ?
# Enum oters :: Type | |
r_std_hashmap/r_std_hashmap_7_0.txt | 1.54.0 · [ source ](../../src/std/collections/hash/map.rs.html#489-491)
#### pub fn into_values (self) -> [ IntoValues
](hash_map/struct.IntoValues.html "struct
std::collections::hash_map::IntoValues") <K, V> â
Creates a consuming iterator visiting all the values in arbitrary order. The
map cannot be used afte... | |
r_std_hashmap/36349_5_0.txt | Find and fix vulnerabilities
* Codespaces | |
r_std_hashmap/enum.Type.html5_21_0.txt | ### Function( Box < Type , Global >, Box < Type , Global >)
§ | |
r_std_hashmap/enum.Type.html5_2_0.txt | * Owners
* PLanza
* Dependencies
* * oters_gui ^0.1.8 normal
* oters_lang ^0.1.8 normal
* oters_macro ^0.1.8 normal
* rand ^0.8.5 normal | |
r_std_hashmap/r_std_hashmap_13_2.txt | ) \+ ? [ Sized
](../marker/trait.Sized.html "trait std::marker::Sized") ,
Returns ` true ` if the map contains a value for the specified key.
The key may be any borrowed form of the mapâs key type, but [ ` Hash `
](../hash/trait.Hash.html "trait std::hash::Hash") and [ ` Eq `
](../cmp/trait.Eq.html "trait std::cmp:... | |
r_std_hashmap/36349_6_0.txt | Instant dev environments
* Copilot | |
r_std_hashmap/enum.Type.html5_36_0.txt | source
#### pub fn well_formed (&self, t_context: TypeContext ) -> Result < ()
, Error > | |
r_std_hashmap/36349_77_0.txt | All reactions
* 👍 2 | |
r_std_hashmap/36349_15_0.txt | * Open Source
* GitHub Sponsors | |
r_std_hashmap/enum.Type.html5_46_0.txt | source §
#### fn fmt (&self, f: &mut Formatter <'_>) -> Result < () , Error > | |
r_std_hashmap/enum.Type.html5_84_0.txt | ### impl<T, U> TryFrom <U> for T where U: Into <T>,
§ | |
r_std_hashmap/36349_25_0.txt | Query
To see all available qualifiers, see our documentation . | |
r_std_hashmap/r_std_hashmap_5_0.txt | 1.54.0 · [ source ](../../src/std/collections/hash/map.rs.html#394-396)
#### pub fn into_keys (self) -> [ IntoKeys ](hash_map/struct.IntoKeys.html
"struct std::collections::hash_map::IntoKeys") <K, V> â
Creates a consuming iterator visiting all the keys in arbitrary order. The map
cannot be used after calling ... | |
r_std_hashmap/36349_75_0.txt | Maintainer
\- | |
r_std_hashmap/enum.Type.html5_60_0.txt | #### fn type_id (&self) -> TypeId
Gets the ` TypeId ` of ` self ` . Read more | |
r_std_hashmap/614907_22_0.txt | (e) Ah, I realize my version is basically what you had. Taking ownership otoh
means knowing who owns the strings in the first place, which is indeed the
question "where's that ` BTreeSet ` from".
agiuliano June 24, 2021, 3:21pm 7 | |
r_std_hashmap/614907_41_0.txt |
let stuff_from_file = read_file(path);
let ordered_strings = Odered_Strings::from_filestruct(&stuff_from_file);
and keep around ` stuff_from_file ` to own the strings. | |
r_std_hashmap/36349_67_0.txt | Quote reply
#### |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.