Skip to content

Commit

Permalink
Merge pull request #262 from quartiq/misc
Browse files Browse the repository at this point in the history
misc
  • Loading branch information
jordens authored Nov 18, 2024
2 parents ac09692 + a8a2dac commit 0911404
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions miniconf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ yafnv = "3.0.0"
tokio = { version = "1.38.0", features = ["io-std", "rt", "macros"] }
strum = { version = "0.26.3", features = ["derive"] }
trybuild = { version = "1.0.99", features = ["diff"] }
serde_json = { version = "1.0.133" }

[[test]]
name = "arrays"
Expand Down
3 changes: 2 additions & 1 deletion miniconf/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ impl<M: TreeKey + ?Sized, N, const D: usize> NodeIter<M, N, D> {
///
/// This requires moving `self` to ensure `FusedIterator`.
pub fn root<K: IntoKeys>(mut self, root: K) -> Result<Self, Traversal> {
self.root = self.state.transcode::<M, _>(root)?.depth();
let node = self.state.transcode::<M, _>(root)?;
self.root = node.depth();
self.depth = D + 1;
Ok(self)
}
Expand Down
8 changes: 7 additions & 1 deletion miniconf/src/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,16 @@ impl Packed {
*self = Self::EMPTY;
}

/// Number of bits that can be stored.
#[inline]
pub const fn capacity(&self) -> u32 {
self.0.trailing_zeros()
}

/// Number of bits stored.
#[inline]
pub const fn len(&self) -> u32 {
Self::CAPACITY - self.0.trailing_zeros()
Self::CAPACITY - self.capacity()
}

/// Return the representation aligned to the LSB with the marker bit
Expand Down
4 changes: 2 additions & 2 deletions miniconf/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,15 @@ pub trait TreeDeserialize<'de> {
/// Deserialize a leaf node by its keys.
///
/// ```
/// # #[cfg(feature = "json-core")] {
/// # #[cfg(feature = "derive")] {
/// use miniconf::{IntoKeys, Leaf, TreeDeserialize, TreeKey};
/// #[derive(Default, TreeKey, TreeDeserialize)]
/// struct S {
/// foo: Leaf<u32>,
/// bar: [Leaf<u16>; 2],
/// };
/// let mut s = S::default();
/// let mut de = serde_json_core::de::Deserializer::new(b"7", None);
/// let mut de = serde_json::de::Deserializer::from_slice(b"7");
/// s.deserialize_by_key(["bar", "0"].into_keys(), &mut de)
/// .unwrap();
/// de.end().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions miniconf_derive/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ impl TreeField {
let getter = self.getter(i);
quote_spanned! { self.span()=>
#getter
.and_then(|value|
::miniconf::TreeSerialize::serialize_by_key(value, keys, ser)
.and_then(|item|
::miniconf::TreeSerialize::serialize_by_key(item, keys, ser)
)
}
}
Expand Down
1 change: 0 additions & 1 deletion miniconf_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub fn derive_tree(input: TokenStream) -> TokenStream {
t.tree_any(),
]
.into_iter()
.flatten()
.collect(),
Err(e) => e.write_errors(),
}
Expand Down
37 changes: 11 additions & 26 deletions miniconf_derive/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub struct TreeVariant {
impl TreeVariant {
fn parse(mut self) -> darling::Result<Self> {
assert!(!self.fields.is_struct());
// unnamed fields can only be skipped if they are terminal
while self
.fields
.fields
Expand Down Expand Up @@ -66,7 +65,6 @@ impl Tree {
fn parse(mut self) -> darling::Result<Self> {
match &mut self.data {
Data::Struct(fields) => {
// unnamed fields can only be skipped if they are terminal
while fields
.fields
.last()
Expand All @@ -81,7 +79,7 @@ impl Tree {
if let Some(f) = fields.fields.iter().find(|f| f.skip.is_present()) {
return Err(
// Note(design) If non-terminal fields are skipped, there is a gap in the indices.
// This could be lifted with a index map.
// This could be lifted with an index map.
Error::custom("Can only `skip` terminal tuple struct fields")
.with_span(&f.skip.span()),
);
Expand Down Expand Up @@ -266,7 +264,6 @@ impl Tree {
{
let index = #index?;
#traverse
#[allow(unreachable_code)]
#increment(match index {
#(#traverse_arms ,)*
_ => unreachable!()
Expand Down Expand Up @@ -294,8 +291,6 @@ impl Tree {
S: ::miniconf::Serializer,
{
let index = #index?;
// Note(unreachable) empty structs have diverged by now
#[allow(unreachable_code)]
#increment(match #mat {
#(#arms ,)*
_ => #default
Expand Down Expand Up @@ -336,8 +331,6 @@ impl Tree {
D: ::miniconf::Deserializer<'de>,
{
let index = #index?;
// Note(unreachable) empty structs have diverged by now
#[allow(unreachable_code)]
#increment(match #mat {
#(#arms ,)*
_ => #default
Expand Down Expand Up @@ -365,31 +358,23 @@ impl Tree {
K: ::miniconf::Keys,
{
let index = #index?;
// Note(unreachable) empty structs have diverged by now
#[allow(unreachable_code)]
{
let ret: ::core::result::Result<_, _> = match #mat {
#(#ref_arms ,)*
_ => #default
};
ret #increment
}
let ret: ::core::result::Result<_, _> = match #mat {
#(#ref_arms ,)*
_ => #default
};
ret #increment
}

fn mut_any_by_key<K>(&mut self, mut keys: K) -> ::core::result::Result<&mut dyn ::core::any::Any, ::miniconf::Traversal>
where
K: ::miniconf::Keys,
{
let index = #index?;
// Note(unreachable) empty structs have diverged by now
#[allow(unreachable_code)]
{
let ret: ::core::result::Result<_, _> = match #mat {
#(#mut_arms ,)*
_ => #default
};
ret #increment
}
let ret: ::core::result::Result<_, _> = match #mat {
#(#mut_arms ,)*
_ => #default
};
ret #increment
}
}
}
Expand Down
1 change: 1 addition & 0 deletions py/miniconf-mqtt/miniconf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async def run():
assert path.startswith("/") or not path
value = await interface.get(path)
print(f"{path}={value}")
await interface.close()

asyncio.run(run())

Expand Down
12 changes: 12 additions & 0 deletions py/miniconf-mqtt/miniconf/miniconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ def __init__(self, client: Client, prefix: str):
self.listener = asyncio.create_task(self._listen())
self.subscribed = asyncio.Event()

async def close(self):
"""Cancel the response listener and all in-flight requests"""
self.listener.cancel()
for fut in self._inflight.values():
fut.cancel()
try:
await self.listener
except asyncio.CancelledError:
pass
if len(self._inflight) > 0:
await asyncio.wait(self._inflight.values())

async def _listen(self):
await self.client.subscribe(self.response_topic)
LOGGER.info(f"Subscribed to {self.response_topic}")
Expand Down

0 comments on commit 0911404

Please sign in to comment.