Fixed some clippy warning by adding default implementations (#31989)

* Fixed some clippy warning by adding default implementations

* Updated PR that adds default implementation of structs

* Clean up and extend `Default` implementations

---------

Co-authored-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
komuhangi 2024-04-04 12:33:30 +03:00 committed by GitHub
parent 62a916ce5c
commit df457c43c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 28 additions and 71 deletions

View file

@ -445,7 +445,7 @@ impl HTMLMediaElement {
seeking: Cell::new(false),
resource_url: DomRefCell::new(None),
blob_url: DomRefCell::new(None),
played: DomRefCell::new(TimeRangesContainer::new()),
played: DomRefCell::new(TimeRangesContainer::default()),
audio_tracks_list: Default::default(),
video_tracks_list: Default::default(),
text_tracks_list: Default::default(),
@ -2346,7 +2346,7 @@ impl HTMLMediaElementMethods for HTMLMediaElement {
// https://html.spec.whatwg.org/multipage/#dom-media-buffered
fn Buffered(&self) -> DomRoot<TimeRanges> {
let mut buffered = TimeRangesContainer::new();
let mut buffered = TimeRangesContainer::default();
if let Some(ref player) = *self.player.borrow() {
if let Ok(ranges) = player.lock().unwrap().buffered() {
for range in ranges {

View file

@ -11,7 +11,7 @@ use webgpu::wgpu::id::{
use webgpu::wgpu::identity::IdentityManager;
use webgpu::wgt::Backend;
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct IdentityHub {
adapters: IdentityManager,
devices: IdentityManager,
@ -29,28 +29,7 @@ pub struct IdentityHub {
render_bundles: IdentityManager,
}
impl IdentityHub {
fn new() -> Self {
IdentityHub {
adapters: IdentityManager::default(),
devices: IdentityManager::default(),
buffers: IdentityManager::default(),
bind_groups: IdentityManager::default(),
bind_group_layouts: IdentityManager::default(),
compute_pipelines: IdentityManager::default(),
pipeline_layouts: IdentityManager::default(),
shader_modules: IdentityManager::default(),
command_encoders: IdentityManager::default(),
textures: IdentityManager::default(),
texture_views: IdentityManager::default(),
samplers: IdentityManager::default(),
render_pipelines: IdentityManager::default(),
render_bundles: IdentityManager::default(),
}
}
}
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Identities {
_surface: IdentityManager,
#[cfg(any(target_os = "linux", target_os = "windows"))]
@ -65,21 +44,6 @@ pub struct Identities {
}
impl Identities {
pub fn new() -> Self {
Identities {
_surface: IdentityManager::default(),
#[cfg(any(target_os = "linux", target_os = "windows"))]
vk_hub: IdentityHub::new(),
#[cfg(target_os = "windows")]
dx12_hub: IdentityHub::new(),
#[cfg(target_os = "windows")]
dx11_hub: IdentityHub::new(),
#[cfg(any(target_os = "ios", target_os = "macos"))]
metal_hub: IdentityHub::new(),
dummy_hub: IdentityHub::new(),
}
}
fn select(&mut self, backend: Backend) -> &mut IdentityHub {
match backend {
#[cfg(any(target_os = "linux", target_os = "windows"))]

View file

@ -214,12 +214,6 @@ bitflags! {
}
}
impl NodeFlags {
pub fn new() -> NodeFlags {
NodeFlags::empty()
}
}
/// suppress observers flag
/// <https://dom.spec.whatwg.org/#concept-node-insert>
/// <https://dom.spec.whatwg.org/#concept-node-remove>
@ -1816,15 +1810,12 @@ impl Node {
}
pub fn new_inherited(doc: &Document) -> Node {
Node::new_(NodeFlags::new(), Some(doc))
Node::new_(NodeFlags::empty(), Some(doc))
}
#[allow(crown::unrooted_must_root)]
pub fn new_document_node() -> Node {
Node::new_(
NodeFlags::new() | NodeFlags::IS_IN_DOC | NodeFlags::IS_CONNECTED,
None,
)
Node::new_(NodeFlags::IS_IN_DOC | NodeFlags::IS_CONNECTED, None)
}
#[allow(crown::unrooted_must_root)]

View file

@ -1082,13 +1082,19 @@ pub struct WeakRangeVec {
cell: UnsafeCell<WeakRefVec<Range>>,
}
impl Default for WeakRangeVec {
fn default() -> Self {
WeakRangeVec {
cell: UnsafeCell::new(WeakRefVec::new()),
}
}
}
#[allow(unsafe_code)]
impl WeakRangeVec {
/// Create a new vector of weak references.
pub fn new() -> Self {
WeakRangeVec {
cell: UnsafeCell::new(WeakRefVec::new()),
}
Self::default()
}
/// Whether that vector of ranges is empty.

View file

@ -242,7 +242,7 @@ impl ServiceWorkerGlobalScope {
runtime,
from_devtools_receiver,
closing,
Arc::new(Mutex::new(Identities::new())),
Arc::new(Mutex::new(Identities::default())),
),
task_queue: TaskQueue::new(receiver, own_sender.clone()),
own_sender,

View file

@ -56,16 +56,12 @@ pub enum TimeRangesError {
OutOfRange,
}
#[derive(Clone, Debug, JSTraceable, MallocSizeOf)]
#[derive(Clone, Debug, Default, JSTraceable, MallocSizeOf)]
pub struct TimeRangesContainer {
ranges: Vec<TimeRange>,
}
impl TimeRangesContainer {
pub fn new() -> Self {
Self { ranges: Vec::new() }
}
pub fn len(&self) -> u32 {
self.ranges.len() as u32
}

View file

@ -72,5 +72,5 @@ pub fn init() -> JSEngineSetup {
perform_platform_specific_initialization();
JSEngineSetup::new()
JSEngineSetup::default()
}

View file

@ -411,8 +411,8 @@ impl Deref for Runtime {
pub struct JSEngineSetup(JSEngine);
impl JSEngineSetup {
pub fn new() -> Self {
impl Default for JSEngineSetup {
fn default() -> Self {
let engine = JSEngine::init().unwrap();
*JS_ENGINE.lock().unwrap() = Some(engine.handle());
Self(engine)

View file

@ -855,7 +855,7 @@ impl ScriptThreadFactory for ScriptThread {
}
impl ScriptThread {
pub fn with_layout<'a, T>(
pub fn with_layout<T>(
pipeline_id: PipelineId,
call: impl FnOnce(&mut dyn Layout) -> T,
) -> Result<T, ()> {
@ -1426,7 +1426,7 @@ impl ScriptThread {
node_ids: Default::default(),
is_user_interacting: Cell::new(false),
gpu_id_hub: Arc::new(Mutex::new(Identities::new())),
gpu_id_hub: Arc::new(Mutex::new(Identities::default())),
webgpu_port: RefCell::new(None),
inherited_secure_context: state.inherited_secure_context,
layouts: Default::default(),

View file

@ -13,7 +13,7 @@ fn check(time_ranges: &TimeRangesContainer, expected: &'static str) {
#[test]
fn initial_state() {
let time_ranges = TimeRangesContainer::new();
let time_ranges = TimeRangesContainer::default();
assert_eq!(time_ranges.len(), 0);
assert!(time_ranges.start(0).is_err());
assert!(time_ranges.end(0).is_err());
@ -21,13 +21,13 @@ fn initial_state() {
#[test]
fn error_if_start_is_older_than_end() {
let mut time_ranges = TimeRangesContainer::new();
let mut time_ranges = TimeRangesContainer::default();
assert!(time_ranges.add(2., 1.).is_err());
}
#[test]
fn single_range() {
let mut time_ranges = TimeRangesContainer::new();
let mut time_ranges = TimeRangesContainer::default();
time_ranges.add(1., 2.).unwrap();
check(&time_ranges, "[1,2)");
assert_eq!(time_ranges.start(0).unwrap(), 1.);
@ -36,14 +36,14 @@ fn single_range() {
#[test]
fn add_order() {
let mut time_ranges_a = TimeRangesContainer::new();
let mut time_ranges_a = TimeRangesContainer::default();
for range in vec![(0., 2.), (3., 4.), (5., 100.)].iter() {
time_ranges_a.add(range.0, range.1).unwrap();
}
let expected = "[0,2), [3,4), [5,100)";
check(&time_ranges_a, expected);
let mut time_ranges_b = TimeRangesContainer::new();
let mut time_ranges_b = TimeRangesContainer::default();
// Add the values in time_ranges_a to time_ranges_b in reverse order.
for i in (0..time_ranges_a.len()).rev() {
time_ranges_b
@ -58,7 +58,7 @@ fn add_order() {
#[test]
fn add_overlapping() {
let mut time_ranges = TimeRangesContainer::new();
let mut time_ranges = TimeRangesContainer::default();
time_ranges.add(0., 2.).unwrap();
time_ranges.add(10., 11.).unwrap();