Skip to content

Commit

Permalink
Auto-generate mut methods for type fields
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Aug 20, 2021
1 parent c1896e9 commit 4556ad1
Show file tree
Hide file tree
Showing 5 changed files with 634 additions and 134 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [codec-0.7.1] - 2021-08-22

* Auto-generate mut methods for type fields

## [0.5.0-b.9] - 2021-08-21

* Upgrade to codec 0.7
Expand Down
2 changes: 1 addition & 1 deletion codec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-amqp-codec"
version = "0.7.0"
version = "0.7.1"
description = "AMQP 1.0 Protocol Codec"
authors = ["Nikolay Kim <[email protected]>", "Max Gortman <[email protected]>", "Mike Yagley <[email protected]>"]
license = "MIT/Apache-2.0"
Expand Down
12 changes: 12 additions & 0 deletions codec/codegen/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ impl {{list.name}} {
{{/if}}
{{/if}}
{{/if}}

{{#if field.optional}}
#[inline]
pub fn {{field.name}}_mut(&mut self) -> Option<&mut {{field.ty}}> {
self.{{list.inner}}{{field.name}}.as_mut()
}
{{else}}
#[inline]
pub fn {{field.name}}_mut(&mut self) -> &mut {{field.ty}} {
&mut self.{{list.inner}}{{field.name}}
}
{{/if}}
{{/each}}

{{#if list.transfer}}
Expand Down
28 changes: 20 additions & 8 deletions codec/src/message/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ impl Message {
self.0.application_properties.as_ref()
}

#[inline]
/// Mut ref tp application property
pub fn app_properties_mut(&mut self) -> Option<&mut VecStringMap> {
self.0.application_properties.as_mut()
}

#[inline]
/// Get application property
pub fn app_property(&self, key: &str) -> Option<&Variant> {
Expand Down Expand Up @@ -172,6 +178,12 @@ impl Message {
self.0.message_annotations.as_ref()
}

#[inline]
/// Mut reference to message annotations
pub fn message_annotations_mut(&mut self) -> Option<&mut VecSymbolMap> {
self.0.message_annotations.as_mut()
}

#[inline]
/// Delivery annotations
pub fn delivery_annotations(&self) -> Option<&VecSymbolMap> {
Expand Down Expand Up @@ -377,7 +389,7 @@ mod tests {
msg.encode(&mut buf);

let msg2 = Message::decode(&buf)?.1;
let props = msg2.properties.as_ref().unwrap();
let props = msg2.properties().unwrap();
assert_eq!(props.message_id, Some(1.into()));
Ok(())
}
Expand All @@ -391,7 +403,7 @@ mod tests {
msg.encode(&mut buf);

let msg2 = Message::decode(&buf)?.1;
let props = msg2.application_properties.as_ref().unwrap();
let props = msg2.app_properties().unwrap();
assert_eq!(props[0].0.as_str(), "test");
assert_eq!(props[0].1, Variant::from(1));
Ok(())
Expand Down Expand Up @@ -427,7 +439,7 @@ mod tests {
msg.encode(&mut buf);

let msg2 = Message::decode(&buf)?.1;
assert_eq!(msg2.body.data().unwrap(), &data);
assert_eq!(msg2.body().data().unwrap(), &data);
Ok(())
}

Expand All @@ -439,7 +451,7 @@ mod tests {
assert_eq!(buf, Bytes::from_static(b""));

let msg2 = Message::decode(&buf)?.1;
assert!(msg2.body.data().is_none());
assert!(msg2.body().data().is_none());
Ok(())
}

Expand All @@ -459,11 +471,11 @@ mod tests {
msg.encode(&mut buf);

let msg3 = Message::decode(&buf)?.1;
let msg4 = Message::decode(&msg3.body.data().unwrap())?.1;
assert_eq!(msg1.properties, msg4.properties);
let msg4 = Message::decode(&msg3.body().data().unwrap())?.1;
assert_eq!(msg1.properties(), msg4.properties());

let msg5 = Message::decode(&msg3.body.data[1])?.1;
assert_eq!(msg2.properties, msg5.properties);
let msg5 = Message::decode(&msg3.body().data[1])?.1;
assert_eq!(msg2.properties(), msg5.properties());
Ok(())
}
}
Loading

0 comments on commit 4556ad1

Please sign in to comment.