Skip to content

Commit df2d09f

Browse files
authored
Merge pull request #34 from chemicstry/fix_clippy
Fix clippy and rustfmt warnings
2 parents 6157ede + 88d7c8c commit df2d09f

File tree

8 files changed

+59
-37
lines changed

8 files changed

+59
-37
lines changed

libcamera-meta/src/bin/generate_c.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::Write;
2+
13
use libcamera_meta::{control_ids, property_ids, Control};
24

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

69-
out.iter()
70-
.map(|line| format!("{}{}\n", " ".repeat(indent), line))
71-
.collect()
71+
out.iter().fold(String::new(), |mut output, line| {
72+
writeln!(output, "{}{}", " ".repeat(indent), line).unwrap();
73+
output
74+
})
7275
}
7376

7477
fn generate_controls(controls: &[Control], name: &str) {

libcamera-meta/src/bin/generate_rust.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ fn generate_controls(controls: &[Control], ty: ControlsType) {
9797
printdoc! {"
9898
impl TryFrom<ControlValue> for {ctrl_name} {{
9999
type Error = ControlValueError;
100-
100+
101101
fn try_from(value: ControlValue) -> Result<Self, Self::Error> {{
102-
Self::try_from({ctrl_type}::try_from(value.clone())?).map_err(|_| ControlValueError::UnknownVariant(value))
102+
Self::try_from({ctrl_type}::try_from(value.clone())?)
103+
.map_err(|_| ControlValueError::UnknownVariant(value))
103104
}}
104105
}}
105-
106+
106107
impl From<{ctrl_name}> for ControlValue {{
107108
fn from(val: {ctrl_name}) -> Self {{
108109
ControlValue::from(<{ctrl_type}>::from(val))
@@ -116,12 +117,12 @@ fn generate_controls(controls: &[Control], ty: ControlsType) {
116117
117118
impl Deref for {ctrl_name} {{
118119
type Target = {ctrl_type};
119-
120+
120121
fn deref(&self) -> &Self::Target {{
121122
&self.0
122123
}}
123124
}}
124-
125+
125126
impl DerefMut for {ctrl_name} {{
126127
fn deref_mut(&mut self) -> &mut Self::Target {{
127128
&mut self.0

libcamera/src/framebuffer_allocator.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ struct FrameBufferAllocatorInstance {
1414
ptr: NonNull<libcamera_framebuffer_allocator_t>,
1515
/// List of streams for which buffers were allocated.
1616
/// We use this list to free buffers on drop.
17-
allocated_streams: Mutex<Vec<NonNull<libcamera_stream_t>>>,
17+
allocated_streams: Vec<NonNull<libcamera_stream_t>>,
1818
}
1919

20+
unsafe impl Send for FrameBufferAllocatorInstance {}
21+
2022
impl Drop for FrameBufferAllocatorInstance {
2123
fn drop(&mut self) {
2224
// Free allocated streams
23-
let mut streams = self.allocated_streams.lock().unwrap();
24-
for stream in streams.drain(..) {
25+
for stream in self.allocated_streams.drain(..) {
2526
unsafe {
2627
libcamera_framebuffer_allocator_free(self.ptr.as_ptr(), stream.as_ptr());
2728
}
@@ -32,30 +33,31 @@ impl Drop for FrameBufferAllocatorInstance {
3233
}
3334

3435
pub struct FrameBufferAllocator {
35-
inner: Arc<FrameBufferAllocatorInstance>,
36+
inner: Arc<Mutex<FrameBufferAllocatorInstance>>,
3637
}
3738

3839
impl FrameBufferAllocator {
3940
pub fn new(cam: &Camera<'_>) -> Self {
4041
Self {
41-
inner: Arc::new(FrameBufferAllocatorInstance {
42+
inner: Arc::new(Mutex::new(FrameBufferAllocatorInstance {
4243
ptr: NonNull::new(unsafe { libcamera_framebuffer_allocator_create(cam.ptr.as_ptr()) }).unwrap(),
43-
allocated_streams: Mutex::new(Vec::new()),
44-
}),
44+
allocated_streams: Vec::new(),
45+
})),
4546
}
4647
}
4748

4849
/// Allocate N buffers for a given stream, where N is equal to
4950
/// [StreamConfigurationRef::get_buffer_count()](crate::stream::StreamConfigurationRef::get_buffer_count).
5051
pub fn alloc(&mut self, stream: &Stream) -> io::Result<Vec<FrameBuffer>> {
51-
let ret = unsafe { libcamera_framebuffer_allocator_allocate(self.inner.ptr.as_ptr(), stream.ptr.as_ptr()) };
52+
let mut inner = self.inner.lock().unwrap();
53+
54+
let ret = unsafe { libcamera_framebuffer_allocator_allocate(inner.ptr.as_ptr(), stream.ptr.as_ptr()) };
5255
if ret < 0 {
5356
Err(io::Error::from_raw_os_error(ret))
5457
} else {
55-
self.inner.allocated_streams.lock().unwrap().push(stream.ptr);
58+
inner.allocated_streams.push(stream.ptr);
5659

57-
let buffers =
58-
unsafe { libcamera_framebuffer_allocator_buffers(self.inner.ptr.as_ptr(), stream.ptr.as_ptr()) };
60+
let buffers = unsafe { libcamera_framebuffer_allocator_buffers(inner.ptr.as_ptr(), stream.ptr.as_ptr()) };
5961

6062
let len = unsafe { libcamera_framebuffer_list_size(buffers) };
6163

@@ -86,7 +88,7 @@ impl FrameBufferAllocator {
8688

8789
pub struct FrameBuffer {
8890
ptr: NonNull<libcamera_framebuffer_t>,
89-
_alloc: Arc<FrameBufferAllocatorInstance>,
91+
_alloc: Arc<Mutex<FrameBufferAllocatorInstance>>,
9092
}
9193

9294
impl core::fmt::Debug for FrameBuffer {

libcamera/src/generated/controls.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
//! Generated by `cargo run --bin generate_rust controls`
22
3-
use crate::control::{Control, ControlEntry, DynControlEntry};
4-
use crate::control_value::{ControlValue, ControlValueError};
3+
use std::ops::{Deref, DerefMut};
4+
5+
use num_enum::{IntoPrimitive, TryFromPrimitive};
6+
57
#[allow(unused_imports)]
68
use crate::geometry::{Rectangle, Size};
7-
use num_enum::{IntoPrimitive, TryFromPrimitive};
8-
use std::ops::{Deref, DerefMut};
9+
use crate::{
10+
control::{Control, ControlEntry, DynControlEntry},
11+
control_value::{ControlValue, ControlValueError},
12+
};
913

1014
#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
1115
#[repr(u32)]
@@ -108,7 +112,8 @@ pub enum ControlId {
108112
///
109113
/// \sa AwbEnable
110114
ColourGains = 15,
111-
/// Report the current estimate of the colour temperature, in kelvin, for this frame. The ColourTemperature control can only be returned in metadata.
115+
/// Report the current estimate of the colour temperature, in kelvin, for this frame. The ColourTemperature control
116+
/// can only be returned in metadata.
112117
ColourTemperature = 16,
113118
/// Specify a fixed saturation parameter. Normal saturation is given by
114119
/// the value 1.0; larger values produce more saturated colours; 0.0
@@ -503,11 +508,15 @@ impl Control for AeMeteringMode {}
503508
#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
504509
#[repr(i32)]
505510
pub enum AeConstraintMode {
506-
/// 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.
511+
/// Default constraint mode. This mode aims to balance the exposure of different parts of the image so as to reach
512+
/// a reasonable average level. However, highlights in the image may appear over-exposed and lowlights may appear
513+
/// under-exposed.
507514
ConstraintNormal = 0,
508-
/// 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.
515+
/// Highlight constraint mode. This mode adjusts the exposure levels in order to try and avoid over-exposing the
516+
/// brightest parts (highlights) of an image. Other non-highlight parts of the image may appear under-exposed.
509517
ConstraintHighlight = 1,
510-
/// 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.
518+
/// Shadows constraint mode. This mode adjusts the exposure levels in order to try and avoid under-exposing the
519+
/// dark parts (shadows) of an image. Other normally exposed parts of the image may appear over-exposed.
511520
ConstraintShadows = 2,
512521
/// Custom constraint mode.
513522
ConstraintCustom = 3,
@@ -1002,7 +1011,8 @@ impl ControlEntry for ColourGains {
10021011

10031012
impl Control for ColourGains {}
10041013

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

@@ -1689,7 +1699,8 @@ impl Control for AfSpeed {}
16891699
pub enum AfMetering {
16901700
/// The AF algorithm should decide for itself where it will measure focus.
16911701
Auto = 0,
1692-
/// 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.
1702+
/// The AF algorithm should use the rectangles defined by the AfWindows control to measure focus. If no windows are
1703+
/// specified the behaviour is platform dependent.
16931704
Windows = 1,
16941705
}
16951706

libcamera/src/generated/properties.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
//! Generated by `cargo run --bin generate_rust properties`
22
3-
use crate::control::{ControlEntry, DynControlEntry, Property};
4-
use crate::control_value::{ControlValue, ControlValueError};
3+
use std::ops::{Deref, DerefMut};
4+
5+
use num_enum::{IntoPrimitive, TryFromPrimitive};
6+
57
#[allow(unused_imports)]
68
use crate::geometry::{Rectangle, Size};
7-
use num_enum::{IntoPrimitive, TryFromPrimitive};
8-
use std::ops::{Deref, DerefMut};
9+
use crate::{
10+
control::{ControlEntry, DynControlEntry, Property},
11+
control_value::{ControlValue, ControlValueError},
12+
};
913

1014
#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
1115
#[repr(u32)]

libcamera/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ pub mod request;
1414
pub mod stream;
1515
pub mod utils;
1616

17-
#[rustfmt::skip]
1817
mod generated;
1918
pub use generated::*;

regenerate.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ cargo run --bin generate_c > libcamera-sys/c_api/controls_generated.h
55
# This could be automated with a procedural macro, but it makes code hard to read and explore.
66
cargo run --bin generate_rust controls > libcamera/src/generated/controls.rs
77
cargo run --bin generate_rust properties > libcamera/src/generated/properties.rs
8-
cargo fmt
8+
cargo +nightly fmt --all

rustfmt.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ comment_width = 120
44
wrap_comments = true
55
format_code_in_doc_comments = true
66
group_imports = "StdExternalCrate"
7-
imports_granularity = "Crate"
7+
imports_granularity = "Crate"
8+
error_on_line_overflow = true
9+
error_on_unformatted = true

0 commit comments

Comments
 (0)