Repository: google/OpenSK
File: src/lib.rs (Rust)

Code before:
    /// Instantiates a CTAP implementation given its environment.
    // This should only take the environment, but it temporarily takes the boot time until the
    // clock is part of the environment.
    pub fn new(mut env: E, now: CtapInstant) -> Self {
        let state = CtapState::new(&mut env, now);
        let hid = MainHid::new();
        #[cfg(feature = "vendor_hid")]
        let vendor_hid = VendorHid::new();
        Ctap {
            env,
            state,
            hid,
            #[cfg(feature = "vendor_hid")]
            vendor_hid,
        }
    }

    pub fn state(&mut self) -> &mut CtapState {
        &mut self.state
    }

    pub fn hid(&mut self) -> &mut MainHid {
        &mut self.hid
    }

    #[cfg(feature = "fuzz")]
    pub fn env(&mut self) -> &mut E {
        &mut self.env
    }

    pub fn process_hid_packet(
        &mut self,
        packet: &HidPacket,
        transport: Transport,
        now: CtapInstant,
    ) -> HidPacketIterator {
        match transport {
            Transport::MainHid => {
                self.hid
                    .process_hid_packet(&mut self.env, packet, now, &mut self.state)
            }
            #[cfg(feature = "vendor_hid")]
            Transport::VendorHid => {
                self.vendor_hid
                    .process_hid_packet(&mut self.env, packet, now, &mut self.state)
            }
        }
    }

    pub fn update_timeouts(&mut self, now: CtapInstant) {
        self.state.update_timeouts(now);

Review comment (refactor):
@ia0 I don't really like exposing this here. But otherwise when using assembler individually (like in fuzz helper) we'll have to pass another env as parameter. But I think we should use the same env. Any better ways?

Code after:
    /// Instantiates a CTAP implementation given its environment.
    // This should only take the environment, but it temporarily takes the boot time until the
    // clock is part of the environment.
    pub fn new(mut env: E, now: CtapInstant) -> Self {
        let state = CtapState::new(&mut env, now);
        let hid = MainHid::new();
        #[cfg(feature = "vendor_hid")]
        let vendor_hid = VendorHid::new();
        Ctap {
            env,
            state,
            hid,
            #[cfg(feature = "vendor_hid")]
            vendor_hid,
        }
    }

    pub fn state(&mut self) -> &mut CtapState {
        &mut self.state
    }

    pub fn hid(&mut self) -> &mut MainHid {
        &mut self.hid
    }

    #[cfg(feature = "std")]
    pub fn env(&mut self) -> &mut E {
        &mut self.env
    }

    pub fn process_hid_packet(
        &mut self,
        packet: &HidPacket,
        transport: Transport,
        now: CtapInstant,
    ) -> HidPacketIterator {
        match transport {
            Transport::MainHid => {
                self.hid
                    .process_hid_packet(&mut self.env, packet, now, &mut self.state)
            }
            #[cfg(feature = "vendor_hid")]
            Transport::VendorHid => {
                self.vendor_hid
                    .process_hid_packet(&mut self.env, packet, now, &mut self.state)
            }
        }
    }

    pub fn update_timeouts(&mut self, now: CtapInstant) {
        self.state.update_timeouts(now);