Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@qwandor
Copy link
Contributor

@qwandor qwandor commented Apr 5, 2022

Pull Request Overview

Default is useful to construct a FieldValue which can be passed to
modify as a no-op or used as an additive identity when combining other
FieldValues.

Eq and PartialEq are useful for assertions in testing.

Testing Strategy

This pull request was tested by cargo test.

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

@github-actions github-actions bot added the tock-libraries This affects libraries supported by the Tock project label Apr 5, 2022
Default is useful to construct a FieldValue which can be passed to
`modify` as a no-op or used as an additive identity when combining other
FieldValues.

Eq and PartialEq are useful for assertions in testing.
@qwandor qwandor force-pushed the fieldvaluetraits branch from 3755003 to fd5687d Compare April 5, 2022 19:26
@lschuermann
Copy link
Member

I don't see the benefit of having a first-class additive identity for FieldValues in mind. Do you have a particular usecase? I'm hesitant, as it might not be immediately clear that a default FieldValue is not just some zero-value, but in fact has a zero mask.

Also, I'm not convinced we should make FieldValue comparable. Given this implicitly compares both the mask and value, it combines two fundamentally distinct operations into a single, opaque and undocumented one. As a user, I could for instance think that the implementation would instead check that the values contained with two FieldValues are identical, compensating for any shift. As we already have the enum abstraction, can we just use that for comparing a given field's value instead (i.e. add #[derive(Eq)])? This has the field encoded in the type information instead.

@qwandor
Copy link
Contributor Author

qwandor commented Apr 6, 2022

There are several usecases I see for Default.

  1. Setting all fields in a register to 0 in a type-safe way, like
registers.reg.write(REG::default());

(Yes, I realise you could just use registers.reg.set(0), but I'd rather set expose the raw setters. In my actual code I have a wrapper around write.)

  1. Accumulating a series of updates to make in one go, like
let mut field_values = REG::default();

if some_condition {
  field_values += REG::FIELD::SET;
}

if some_other_condition {
  field_values += REG::OTHER_FIELD::SET;
}

registers.reg.modify(field_values);

As for Eq, I mostly had in mind using it for testing, so I could use assert_eq! with values passed to a mock, but it's less important to me right now.

Derive Copy, Clone, Eq and PartialEq for generated field value enums.
@lschuermann
Copy link
Member

Okay, I see the benefit of having a default implementation for FieldValues now. I'm not super happy with the name still, as it's not immediately clear what "default" implies. Especially for memory-mapped registers, default is a common phrase to refer to the reset-values of a given register, which aren't necessarily zero. It seems entirely reasonable and super useful for some cases to introduce custom register types which have a proper const-generic default value attached to them, representing the reset value of this register, and perhaps also a mask of writeable vs. reserved bits. In that spirit, would it perhaps be better to call this associated function none or empty?

@qwandor
Copy link
Contributor Author

qwandor commented Apr 6, 2022

Implementing the Default trait has the advantage that it can then be derived on containing structs (i.e. if I make a struct that contains a FieldValue), and also seems more idiomatic to me, but a none or empty method would work for my immediate usecase. I think none is probably a better name than empty as it represents modifying none of the fields.

@alistair23
Copy link
Contributor

I agree, Default implies the reset value so none of empty seem like better names

@hudson-ayers
Copy link
Contributor

I might even recommend zeroed(), though I think none or empty are fine as well

@qwandor
Copy link
Contributor Author

qwandor commented Apr 7, 2022

Done. zeroed sounds to me like it would result in setting some fields to 0, which is not what this does, so I've gone with none.

alistair23
alistair23 previously approved these changes Apr 7, 2022
@lschuermann
Copy link
Member

As for Eq, I mostly had in mind using it for testing, so I could use assert_eq! with values passed to a mock, but it's less important to me right now.

Could you perhaps remove this change from this PR? I'm still not 100% convinced on that and it seems it wouldn't be a major inconvenience to you to just compare the mask and the read value (in conjuction with the corresponding Field type), if this matches your definition of equality?

@qwandor
Copy link
Contributor Author

qwandor commented Apr 8, 2022

Done.

@lschuermann lschuermann changed the title Derive Default, Eq and PartialEq for FieldValue. tock-register-interface: Provide none method for FieldValue type. Apr 9, 2022
@lschuermann
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Apr 9, 2022

@bors bors bot merged commit da015b6 into tock:master Apr 9, 2022
ppannuto added a commit that referenced this pull request Aug 19, 2022
`tock-registers` now supports stable Rust!

There is a small breaking change, documented below, required to support
Rust 2021 edition. Most of the remaining changes are improvements to the
internal self-testing infrastructure and documentation. There are also
some additions to `FieldValue` to improve ergonomics.

**Breaking Changes**

 - #2842: tock-registers: rename TryFromValue::try_from to try_from_value
 - #2833: Update to Rust 2021 edition

Other Changes

 - #3126: [trivial] tock-registers: mark two methods as `const`
 - #3088: tock_registers/test_fields: respect struct size padding w/ alignment
 - #3072: Update Rust nightly version + Expose virtual-function-elimination
     - libraries/tock-register-interface: Fixup register_structs documentation
 - #2988: Remove const_fn_trait_bound feature and update nightly (Mar 2022)
 - #3014: tock-registers: Implement From field enum value type for FieldValue
 - #3013: tock-register-interface: Provide none method for FieldValue type
 - #2916: tock-register-interface: improve read_as_enum documentation
 - #2922: tock-register-interface: replace register tests by const assertions
ppannuto added a commit that referenced this pull request Aug 19, 2022
`tock-registers` now supports stable Rust!

There is a small breaking change, documented below, required to support
Rust 2021 edition. Most of the remaining changes are improvements to the
internal self-testing infrastructure and documentation. There are also
some additions to `FieldValue` to improve ergonomics.

**Breaking Changes**

 - #2842: tock-registers: rename TryFromValue::try_from to try_from_value
 - #2833: Update to Rust 2021 edition

Other Changes

 - #3126: [trivial] tock-registers: mark two methods as `const`
 - #3088: tock_registers/test_fields: respect struct size padding w/ alignment
 - #3072: Update Rust nightly version + Expose virtual-function-elimination
     - libraries/tock-register-interface: Fixup register_structs documentation
 - #2988: Remove const_fn_trait_bound feature and update nightly (Mar 2022)
 - #3014: tock-registers: Implement From field enum value type for FieldValue
 - #3013: tock-register-interface: Provide none method for FieldValue type
 - #2916: tock-register-interface: improve read_as_enum documentation
 - #2922: tock-register-interface: replace register tests by const assertions
@ppannuto ppannuto mentioned this pull request Aug 19, 2022
2 tasks
ppannuto added a commit that referenced this pull request Aug 19, 2022
`tock-registers` now supports stable Rust!

There is a small breaking change, documented below, required to support
Rust 2021 edition. Most of the remaining changes are improvements to the
internal self-testing infrastructure and documentation. There are also
some additions to `FieldValue` to improve ergonomics.

**Breaking Changes**

 - #2842: tock-registers: rename TryFromValue::try_from to try_from_value
 - #2838: Update to Rust 2021 edition

Other Changes

 - #3126: [trivial] tock-registers: mark two methods as `const`
 - #3088: tock_registers/test_fields: respect struct size padding w/ alignment
 - #3072: Update Rust nightly version + Expose virtual-function-elimination
     - libraries/tock-register-interface: Fixup register_structs documentation
 - #2988: Remove const_fn_trait_bound feature and update nightly (Mar 2022)
 - #3014: tock-registers: Implement From field enum value type for FieldValue
 - #3013: tock-register-interface: Provide none method for FieldValue type
 - #2916: tock-register-interface: improve read_as_enum documentation
 - #2922: tock-register-interface: replace register tests by const assertions
bors bot added a commit that referenced this pull request Aug 24, 2022
3151: tock-registers: release v0.8 r=lschuermann a=ppannuto

### Pull Request Overview

Prepare a v0.8 release of tock-registers.

Fixes #2982.

---

`tock-registers` now supports stable Rust!

There is a small breaking change, documented below, required to support
Rust 2021 edition. Most of the remaining changes are improvements to the
internal self-testing infrastructure and documentation. There are also
some additions to `FieldValue` to improve ergonomics.

**Breaking Changes**

 - #2842: tock-registers: rename TryFromValue::try_from to try_from_value
 - #2838: Update to Rust 2021 edition

Other Changes

 - #3126: [trivial] tock-registers: mark two methods as `const`
 - #3088: tock_registers/test_fields: respect struct size padding w/ alignment
 - #3072: Update Rust nightly version + Expose virtual-function-elimination
     - libraries/tock-register-interface: Fixup register_structs documentation
 - #2988: Remove const_fn_trait_bound feature and update nightly (Mar 2022)
 - #3014: tock-registers: Implement From field enum value type for FieldValue
 - #3013: tock-register-interface: Provide none method for FieldValue type
 - #2916: tock-register-interface: improve read_as_enum documentation
 - #2922: tock-register-interface: replace register tests by const assertions

### Testing Strategy

N/A

### TODO or Help Wanted

N/A

### Documentation Updated

- [x] Updated the relevant files in `/docs`, or no updates are required.

### Formatting

- [ ] Ran `make prepush`.


Co-authored-by: Pat Pannuto <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tock-libraries This affects libraries supported by the Tock project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants