Skip to content

Commit

Permalink
feat: Apply const keyword to many methods (#167)
Browse files Browse the repository at this point in the history
Apply the const keyword to various methods based on feedback given by the clippy missing_const_for_fn check.

This patch also enables `clippy::missing_const_for_fn` check as a warning.

Signed-off-by: MTRNord <[email protected]>
  • Loading branch information
MTRNord authored Aug 8, 2024
1 parent 417fb77 commit 72f4235
Show file tree
Hide file tree
Showing 27 changed files with 71 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/cipher/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl CipherKeys {
Aes256Key::from_slice(self.aes_key.as_slice())
}

pub fn mac_key(&self) -> &HmacSha256Key {
pub const fn mac_key(&self) -> &HmacSha256Key {
&self.mac_key
}

Expand Down
10 changes: 5 additions & 5 deletions src/ecies/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct EciesNonce {

impl EciesNonce {
/// Create a new [`EciesNonce`], starting the count from 0.
fn new() -> Self {
const fn new() -> Self {
Self { inner: 0 }
}

Expand Down Expand Up @@ -161,7 +161,7 @@ impl CheckCode {
///
/// The bytes can be converted to a more user-friendly representation. The
/// [`CheckCode::to_digit`] converts the bytes to a two-digit number.
pub fn as_bytes(&self) -> &[u8; 2] {
pub const fn as_bytes(&self) -> &[u8; 2] {
&self.bytes
}

Expand All @@ -179,7 +179,7 @@ impl CheckCode {
///
/// println!("The check code of the IECS channel is: {check_code:02}");
/// ```
pub fn to_digit(&self) -> u8 {
pub const fn to_digit(&self) -> u8 {
let first = (self.bytes[0] % 10) * 10;
let second = self.bytes[1] % 10;

Expand Down Expand Up @@ -472,7 +472,7 @@ impl EstablishedEcies {
///
/// This public key needs to be sent to the other side so that it can
/// complete the ECIES channel establishment.
pub fn public_key(&self) -> Curve25519PublicKey {
pub const fn public_key(&self) -> Curve25519PublicKey {
self.our_public_key
}

Expand All @@ -481,7 +481,7 @@ impl EstablishedEcies {
///
/// This check code can be used to check that both sides of the session are
/// indeed using the same shared secret.
pub fn check_code(&self) -> &CheckCode {
pub const fn check_code(&self) -> &CheckCode {
&self.check_code
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
unused_qualifications,
rust_2018_idioms
)]
#![warn(clippy::missing_const_for_fn)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

mod cipher;
Expand Down
4 changes: 2 additions & 2 deletions src/megolm/group_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ impl GroupSession {
///
/// The message index is incremented each time a message is encrypted with
/// the group session.
pub fn message_index(&self) -> u32 {
pub const fn message_index(&self) -> u32 {
self.ratchet.index()
}

pub fn session_config(&self) -> SessionConfig {
pub const fn session_config(&self) -> SessionConfig {
self.config
}

Expand Down
2 changes: 1 addition & 1 deletion src/megolm/inbound_group_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl InboundGroupSession {
})
}

pub fn first_known_index(&self) -> u32 {
pub const fn first_known_index(&self) -> u32 {
self.initial_ratchet.index()
}

Expand Down
4 changes: 2 additions & 2 deletions src/megolm/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl MegolmMessage {
}

/// The index of the message that was used when the message was encrypted.
pub fn message_index(&self) -> u32 {
pub const fn message_index(&self) -> u32 {
self.message_index
}

Expand All @@ -65,7 +65,7 @@ impl MegolmMessage {
}

/// Get a reference to the megolm message's signature.
pub fn signature(&self) -> &Ed25519Signature {
pub const fn signature(&self) -> &Ed25519Signature {
&self.signature
}

Expand Down
2 changes: 1 addition & 1 deletion src/megolm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use message::MegolmMessage;
pub use session_config::SessionConfig;
pub use session_keys::{ExportedSessionKey, SessionKey, SessionKeyDecodeError};

fn default_config() -> SessionConfig {
const fn default_config() -> SessionConfig {
SessionConfig::version_1()
}

Expand Down
6 changes: 3 additions & 3 deletions src/megolm/ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ impl Ratchet {
ratchet
}

pub fn from_bytes(bytes: Box<[u8; Self::RATCHET_LENGTH]>, counter: u32) -> Self {
pub const fn from_bytes(bytes: Box<[u8; Self::RATCHET_LENGTH]>, counter: u32) -> Self {
Self { inner: RatchetBytes(bytes), counter }
}

pub fn index(&self) -> u32 {
pub const fn index(&self) -> u32 {
self.counter
}

pub fn as_bytes(&self) -> &[u8; Self::RATCHET_LENGTH] {
pub const fn as_bytes(&self) -> &[u8; Self::RATCHET_LENGTH] {
&self.inner.0
}

Expand Down
6 changes: 3 additions & 3 deletions src/megolm/session_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ pub(super) enum Version {

impl SessionConfig {
/// Get the numeric version of this `SessionConfig`.
pub fn version(&self) -> u8 {
pub const fn version(&self) -> u8 {
self.version as u8
}

/// Create a `SessionConfig` for the Megolm version 1. This version of
/// Megolm uses AES-256 and HMAC with a truncated MAC to encrypt individual
/// messages. The MAC will be truncated to 8 bytes.
pub fn version_1() -> Self {
pub const fn version_1() -> Self {
SessionConfig { version: Version::V1 }
}

/// Create a `SessionConfig` for the Megolm version 2. This version of
/// Megolm uses AES-256 and HMAC to encrypt individual messages. The MAC
/// won't be truncated.
pub fn version_2() -> Self {
pub const fn version_2() -> Self {
SessionConfig { version: Version::V2 }
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/olm/account/fallback_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ impl FallbackKey {
Curve25519PublicKey::from(&self.key)
}

pub fn secret_key(&self) -> &Curve25519SecretKey {
pub const fn secret_key(&self) -> &Curve25519SecretKey {
&self.key
}

pub fn key_id(&self) -> KeyId {
pub const fn key_id(&self) -> KeyId {
self.key_id
}

pub fn mark_as_published(&mut self) {
self.published = true;
}

pub fn published(&self) -> bool {
pub const fn published(&self) -> bool {
self.published
}
}
Expand All @@ -62,7 +62,7 @@ pub(super) struct FallbackKeys {
}

impl FallbackKeys {
pub fn new() -> Self {
pub const fn new() -> Self {
Self { key_id: 0, fallback_key: None, previous_fallback_key: None }
}

Expand Down
8 changes: 4 additions & 4 deletions src/olm/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ impl Account {
}

/// Get the IdentityKeys of this Account
pub fn identity_keys(&self) -> IdentityKeys {
pub const fn identity_keys(&self) -> IdentityKeys {
IdentityKeys { ed25519: self.ed25519_key(), curve25519: self.curve25519_key() }
}

/// Get a reference to the account's public Ed25519 key
pub fn ed25519_key(&self) -> Ed25519PublicKey {
pub const fn ed25519_key(&self) -> Ed25519PublicKey {
self.signing_key.public_key()
}

/// Get a reference to the account's public Curve25519 key
pub fn curve25519_key(&self) -> Curve25519PublicKey {
pub const fn curve25519_key(&self) -> Curve25519PublicKey {
self.diffie_hellman_key.public_key()
}

Expand All @@ -139,7 +139,7 @@ impl Account {
/// **Note**: this differs from the libolm method of the same name, the
/// libolm method returned the maximum amount of one-time keys the `Account`
/// could hold and only half of those should be uploaded.
pub fn max_number_of_one_time_keys(&self) -> usize {
pub const fn max_number_of_one_time_keys(&self) -> usize {
// We tell clients to upload a limited amount of one-time keys, this
// amount is smaller than what we can store.
//
Expand Down
2 changes: 1 addition & 1 deletion src/olm/account/one_time_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl OneTimeKeys {
self.insert_secret_key(key_id, key, false)
}

pub(crate) fn secret_keys(&self) -> &BTreeMap<KeyId, Curve25519SecretKey> {
pub(crate) const fn secret_keys(&self) -> &BTreeMap<KeyId, Curve25519SecretKey> {
&self.private_keys
}

Expand Down
8 changes: 4 additions & 4 deletions src/olm/messages/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ pub struct Message {
impl Message {
/// The public part of the ratchet key, that was used when the message was
/// encrypted.
pub fn ratchet_key(&self) -> Curve25519PublicKey {
pub const fn ratchet_key(&self) -> Curve25519PublicKey {
self.ratchet_key
}

/// The index of the chain that was used when the message was encrypted.
pub fn chain_index(&self) -> u64 {
pub const fn chain_index(&self) -> u64 {
self.chain_index
}

Expand All @@ -59,12 +59,12 @@ impl Message {
}

/// The version of the Olm message.
pub fn version(&self) -> u8 {
pub const fn version(&self) -> u8 {
self.version
}

/// Has the MAC been truncated in this Olm message.
pub fn mac_truncated(&self) -> bool {
pub const fn mac_truncated(&self) -> bool {
self.version == MAC_TRUNCATED_VERSION
}

Expand Down
2 changes: 1 addition & 1 deletion src/olm/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl OlmMessage {
}

/// Get the type of the message.
pub fn message_type(&self) -> MessageType {
pub const fn message_type(&self) -> MessageType {
match self {
OlmMessage::Normal(_) => MessageType::Normal,
OlmMessage::PreKey(_) => MessageType::PreKey,
Expand Down
12 changes: 6 additions & 6 deletions src/olm/messages/pre_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ impl PreKeyMessage {
/// receiver of the message. Should be used to establish a [`Session`].
///
/// [`Session`]: crate::olm::Session
pub fn one_time_key(&self) -> Curve25519PublicKey {
pub const fn one_time_key(&self) -> Curve25519PublicKey {
self.session_keys.one_time_key
}

/// The base key, a single use key that was created just in time by the
/// sender of the message. Should be used to establish a [`Session`].
///
/// [`Session`]: crate::olm::Session
pub fn base_key(&self) -> Curve25519PublicKey {
pub const fn base_key(&self) -> Curve25519PublicKey {
self.session_keys.base_key
}

/// The long term identity key of the sender of the message. Should be used
/// to establish a [`Session`]
///
/// [`Session`]: crate::olm::Session
pub fn identity_key(&self) -> Curve25519PublicKey {
pub const fn identity_key(&self) -> Curve25519PublicKey {
self.session_keys.identity_key
}

Expand All @@ -70,7 +70,7 @@ impl PreKeyMessage {
/// can be used to retrieve individual keys from this collection.
///
/// [`Session`]: crate::olm::Session
pub fn session_keys(&self) -> SessionKeys {
pub const fn session_keys(&self) -> SessionKeys {
self.session_keys
}

Expand All @@ -82,7 +82,7 @@ impl PreKeyMessage {
}

/// The actual message that contains the ciphertext.
pub fn message(&self) -> &Message {
pub const fn message(&self) -> &Message {
&self.message
}

Expand Down Expand Up @@ -157,7 +157,7 @@ impl PreKeyMessage {
PreKeyMessage::new(session_keys, message)
}

pub(crate) fn new(session_keys: SessionKeys, message: Message) -> Self {
pub(crate) const fn new(session_keys: SessionKeys, message: Message) -> Self {
Self { session_keys, message }
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/olm/session/chain_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ pub(super) struct RemoteChainKey {
}

impl RemoteChainKey {
pub fn new(bytes: Box<[u8; 32]>) -> Self {
pub const fn new(bytes: Box<[u8; 32]>) -> Self {
Self { key: bytes, index: 0 }
}

pub fn chain_index(&self) -> u64 {
pub const fn chain_index(&self) -> u64 {
self.index
}

Expand All @@ -91,7 +91,7 @@ impl RemoteChainKey {
}

impl ChainKey {
pub fn new(bytes: Box<[u8; 32]>) -> Self {
pub const fn new(bytes: Box<[u8; 32]>) -> Self {
Self { key: bytes, index: 0 }
}

Expand All @@ -106,7 +106,7 @@ impl ChainKey {
self.index += 1;
}

pub fn index(&self) -> u64 {
pub const fn index(&self) -> u64 {
self.index
}

Expand Down
4 changes: 2 additions & 2 deletions src/olm/session/double_ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ pub enum RatchetCount {
}

impl RatchetCount {
pub fn new() -> RatchetCount {
pub const fn new() -> RatchetCount {
RatchetCount::Known(0)
}

pub fn unknown() -> RatchetCount {
pub const fn unknown() -> RatchetCount {
RatchetCount::Unknown(())
}

Expand Down
6 changes: 3 additions & 3 deletions src/olm/session/message_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Drop for RemoteMessageKey {
}

impl MessageKey {
pub fn new(key: Box<[u8; 32]>, ratchet_key: RatchetPublicKey, index: u64) -> Self {
pub const fn new(key: Box<[u8; 32]>, ratchet_key: RatchetPublicKey, index: u64) -> Self {
Self { key, ratchet_key, index }
}

Expand Down Expand Up @@ -107,11 +107,11 @@ impl MessageKey {
}

impl RemoteMessageKey {
pub fn new(key: Box<[u8; 32]>, index: u64) -> Self {
pub const fn new(key: Box<[u8; 32]>, index: u64) -> Self {
Self { key, index }
}

pub fn chain_index(&self) -> u64 {
pub const fn chain_index(&self) -> u64 {
self.index
}

Expand Down
Loading

0 comments on commit 72f4235

Please sign in to comment.