Skip to content

Commit

Permalink
Merge pull request #34 from chemicstry/fix_clippy
Browse files Browse the repository at this point in the history
Fix clippy and rustfmt warnings
  • Loading branch information
chemicstry authored Nov 9, 2023
2 parents 6157ede + 88d7c8c commit df2d09f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 37 deletions.
9 changes: 6 additions & 3 deletions libcamera-meta/src/bin/generate_c.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use libcamera_meta::{control_ids, property_ids, Control};

/// Converts `ExampleName` to `example_name`
Expand Down Expand Up @@ -66,9 +68,10 @@ fn format_docstring(desc: &str, indent: usize) -> String {
}
out.push(" */".to_string());

out.iter()
.map(|line| format!("{}{}\n", " ".repeat(indent), line))
.collect()
out.iter().fold(String::new(), |mut output, line| {
writeln!(output, "{}{}", " ".repeat(indent), line).unwrap();
output
})
}

fn generate_controls(controls: &[Control], name: &str) {
Expand Down
11 changes: 6 additions & 5 deletions libcamera-meta/src/bin/generate_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ fn generate_controls(controls: &[Control], ty: ControlsType) {
printdoc! {"
impl TryFrom<ControlValue> for {ctrl_name} {{
type Error = ControlValueError;
fn try_from(value: ControlValue) -> Result<Self, Self::Error> {{
Self::try_from({ctrl_type}::try_from(value.clone())?).map_err(|_| ControlValueError::UnknownVariant(value))
Self::try_from({ctrl_type}::try_from(value.clone())?)
.map_err(|_| ControlValueError::UnknownVariant(value))
}}
}}
impl From<{ctrl_name}> for ControlValue {{
fn from(val: {ctrl_name}) -> Self {{
ControlValue::from(<{ctrl_type}>::from(val))
Expand All @@ -116,12 +117,12 @@ fn generate_controls(controls: &[Control], ty: ControlsType) {
impl Deref for {ctrl_name} {{
type Target = {ctrl_type};
fn deref(&self) -> &Self::Target {{
&self.0
}}
}}
impl DerefMut for {ctrl_name} {{
fn deref_mut(&mut self) -> &mut Self::Target {{
&mut self.0
Expand Down
26 changes: 14 additions & 12 deletions libcamera/src/framebuffer_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ struct FrameBufferAllocatorInstance {
ptr: NonNull<libcamera_framebuffer_allocator_t>,
/// List of streams for which buffers were allocated.
/// We use this list to free buffers on drop.
allocated_streams: Mutex<Vec<NonNull<libcamera_stream_t>>>,
allocated_streams: Vec<NonNull<libcamera_stream_t>>,
}

unsafe impl Send for FrameBufferAllocatorInstance {}

impl Drop for FrameBufferAllocatorInstance {
fn drop(&mut self) {
// Free allocated streams
let mut streams = self.allocated_streams.lock().unwrap();
for stream in streams.drain(..) {
for stream in self.allocated_streams.drain(..) {
unsafe {
libcamera_framebuffer_allocator_free(self.ptr.as_ptr(), stream.as_ptr());
}
Expand All @@ -32,30 +33,31 @@ impl Drop for FrameBufferAllocatorInstance {
}

pub struct FrameBufferAllocator {
inner: Arc<FrameBufferAllocatorInstance>,
inner: Arc<Mutex<FrameBufferAllocatorInstance>>,
}

impl FrameBufferAllocator {
pub fn new(cam: &Camera<'_>) -> Self {
Self {
inner: Arc::new(FrameBufferAllocatorInstance {
inner: Arc::new(Mutex::new(FrameBufferAllocatorInstance {
ptr: NonNull::new(unsafe { libcamera_framebuffer_allocator_create(cam.ptr.as_ptr()) }).unwrap(),
allocated_streams: Mutex::new(Vec::new()),
}),
allocated_streams: Vec::new(),
})),
}
}

/// Allocate N buffers for a given stream, where N is equal to
/// [StreamConfigurationRef::get_buffer_count()](crate::stream::StreamConfigurationRef::get_buffer_count).
pub fn alloc(&mut self, stream: &Stream) -> io::Result<Vec<FrameBuffer>> {
let ret = unsafe { libcamera_framebuffer_allocator_allocate(self.inner.ptr.as_ptr(), stream.ptr.as_ptr()) };
let mut inner = self.inner.lock().unwrap();

let ret = unsafe { libcamera_framebuffer_allocator_allocate(inner.ptr.as_ptr(), stream.ptr.as_ptr()) };
if ret < 0 {
Err(io::Error::from_raw_os_error(ret))
} else {
self.inner.allocated_streams.lock().unwrap().push(stream.ptr);
inner.allocated_streams.push(stream.ptr);

let buffers =
unsafe { libcamera_framebuffer_allocator_buffers(self.inner.ptr.as_ptr(), stream.ptr.as_ptr()) };
let buffers = unsafe { libcamera_framebuffer_allocator_buffers(inner.ptr.as_ptr(), stream.ptr.as_ptr()) };

let len = unsafe { libcamera_framebuffer_list_size(buffers) };

Expand Down Expand Up @@ -86,7 +88,7 @@ impl FrameBufferAllocator {

pub struct FrameBuffer {
ptr: NonNull<libcamera_framebuffer_t>,
_alloc: Arc<FrameBufferAllocatorInstance>,
_alloc: Arc<Mutex<FrameBufferAllocatorInstance>>,
}

impl core::fmt::Debug for FrameBuffer {
Expand Down
31 changes: 21 additions & 10 deletions libcamera/src/generated/controls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! Generated by `cargo run --bin generate_rust controls`
use crate::control::{Control, ControlEntry, DynControlEntry};
use crate::control_value::{ControlValue, ControlValueError};
use std::ops::{Deref, DerefMut};

use num_enum::{IntoPrimitive, TryFromPrimitive};

#[allow(unused_imports)]
use crate::geometry::{Rectangle, Size};
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::ops::{Deref, DerefMut};
use crate::{
control::{Control, ControlEntry, DynControlEntry},
control_value::{ControlValue, ControlValueError},
};

#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
Expand Down Expand Up @@ -108,7 +112,8 @@ pub enum ControlId {
///
/// \sa AwbEnable
ColourGains = 15,
/// Report the current estimate of the colour temperature, in kelvin, for this frame. The ColourTemperature control can only be returned in metadata.
/// Report the current estimate of the colour temperature, in kelvin, for this frame. The ColourTemperature control
/// can only be returned in metadata.
ColourTemperature = 16,
/// Specify a fixed saturation parameter. Normal saturation is given by
/// the value 1.0; larger values produce more saturated colours; 0.0
Expand Down Expand Up @@ -503,11 +508,15 @@ impl Control for AeMeteringMode {}
#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(i32)]
pub enum AeConstraintMode {
/// Default constraint mode. This mode aims to balance the exposure of different parts of the image so as to reach a reasonable average level. However, highlights in the image may appear over-exposed and lowlights may appear under-exposed.
/// Default constraint mode. This mode aims to balance the exposure of different parts of the image so as to reach
/// a reasonable average level. However, highlights in the image may appear over-exposed and lowlights may appear
/// under-exposed.
ConstraintNormal = 0,
/// Highlight constraint mode. This mode adjusts the exposure levels in order to try and avoid over-exposing the brightest parts (highlights) of an image. Other non-highlight parts of the image may appear under-exposed.
/// Highlight constraint mode. This mode adjusts the exposure levels in order to try and avoid over-exposing the
/// brightest parts (highlights) of an image. Other non-highlight parts of the image may appear under-exposed.
ConstraintHighlight = 1,
/// Shadows constraint mode. This mode adjusts the exposure levels in order to try and avoid under-exposing the dark parts (shadows) of an image. Other normally exposed parts of the image may appear over-exposed.
/// Shadows constraint mode. This mode adjusts the exposure levels in order to try and avoid under-exposing the
/// dark parts (shadows) of an image. Other normally exposed parts of the image may appear over-exposed.
ConstraintShadows = 2,
/// Custom constraint mode.
ConstraintCustom = 3,
Expand Down Expand Up @@ -1002,7 +1011,8 @@ impl ControlEntry for ColourGains {

impl Control for ColourGains {}

/// Report the current estimate of the colour temperature, in kelvin, for this frame. The ColourTemperature control can only be returned in metadata.
/// Report the current estimate of the colour temperature, in kelvin, for this frame. The ColourTemperature control can
/// only be returned in metadata.
#[derive(Debug, Clone)]
pub struct ColourTemperature(pub i32);

Expand Down Expand Up @@ -1689,7 +1699,8 @@ impl Control for AfSpeed {}
pub enum AfMetering {
/// The AF algorithm should decide for itself where it will measure focus.
Auto = 0,
/// The AF algorithm should use the rectangles defined by the AfWindows control to measure focus. If no windows are specified the behaviour is platform dependent.
/// The AF algorithm should use the rectangles defined by the AfWindows control to measure focus. If no windows are
/// specified the behaviour is platform dependent.
Windows = 1,
}

Expand Down
12 changes: 8 additions & 4 deletions libcamera/src/generated/properties.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! Generated by `cargo run --bin generate_rust properties`
use crate::control::{ControlEntry, DynControlEntry, Property};
use crate::control_value::{ControlValue, ControlValueError};
use std::ops::{Deref, DerefMut};

use num_enum::{IntoPrimitive, TryFromPrimitive};

#[allow(unused_imports)]
use crate::geometry::{Rectangle, Size};
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::ops::{Deref, DerefMut};
use crate::{
control::{ControlEntry, DynControlEntry, Property},
control_value::{ControlValue, ControlValueError},
};

#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
Expand Down
1 change: 0 additions & 1 deletion libcamera/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ pub mod request;
pub mod stream;
pub mod utils;

#[rustfmt::skip]
mod generated;
pub use generated::*;
2 changes: 1 addition & 1 deletion regenerate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cargo run --bin generate_c > libcamera-sys/c_api/controls_generated.h
# This could be automated with a procedural macro, but it makes code hard to read and explore.
cargo run --bin generate_rust controls > libcamera/src/generated/controls.rs
cargo run --bin generate_rust properties > libcamera/src/generated/properties.rs
cargo fmt
cargo +nightly fmt --all
4 changes: 3 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ comment_width = 120
wrap_comments = true
format_code_in_doc_comments = true
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
imports_granularity = "Crate"
error_on_line_overflow = true
error_on_unformatted = true

0 comments on commit df2d09f

Please sign in to comment.