Switch everything from lazy_static to once_cell::Lazy

Fewer macros, faster compile-time and the Lazy type will likely end up
in the standard library in a similar form to this.
This commit is contained in:
Sebastian Dröge 2020-01-22 19:38:13 +02:00
parent 2b5f16391d
commit 7230aee069
19 changed files with 178 additions and 180 deletions

View file

@ -30,7 +30,7 @@ pango = { git = "https://github.com/gtk-rs/pango", optional = true }
pangocairo = { git = "https://github.com/gtk-rs/pangocairo", optional = true }
glutin = { version = "0.21", optional = true }
winit = { version = "0.19", optional = true }
lazy_static = "1.0"
once_cell = "1.0"
[build-dependencies]
gl_generator = { version = "0.14", optional = true }

View file

@ -42,16 +42,16 @@ mod fir_filter {
extern crate byte_slice_cast;
use byte_slice_cast::*;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
// The debug category we use below for our filter
lazy_static! {
pub static ref CAT: gst::DebugCategory = gst::DebugCategory::new(
pub static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
gst::DebugCategory::new(
"rsfirfilter",
gst::DebugColorFlags::empty(),
Some("Rust FIR Filter"),
);
}
)
});
// In the imp submodule we include the actual implementation
mod imp {

View file

@ -17,7 +17,7 @@ build = "build.rs"
bitflags = "1.0"
byteorder = "1"
libc = "0.2"
lazy_static = "1.0"
once_cell = "1.0"
glib-sys = { git = "https://github.com/gtk-rs/sys" }
gobject-sys = { git = "https://github.com/gtk-rs/sys" }
gstreamer-sys = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs-sys", features = ["v1_14"] }

View file

@ -10,12 +10,12 @@ use gst::CapsFeatures;
use gst_gl_sys;
use std::ffi::CStr;
lazy_static! {
pub static ref CAPS_FEATURE_MEMORY_GL_MEMORY: &'static str = unsafe {
use once_cell::sync::Lazy;
pub static CAPS_FEATURE_MEMORY_GL_MEMORY: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_gl_sys::GST_CAPS_FEATURE_MEMORY_GL_MEMORY)
.to_str()
.unwrap()
};
pub static ref CAPS_FEATURES_MEMORY_GL_MEMORY: CapsFeatures =
CapsFeatures::new(&[*CAPS_FEATURE_MEMORY_GL_MEMORY]);
}
});
pub static CAPS_FEATURES_MEMORY_GL_MEMORY: Lazy<CapsFeatures> =
Lazy::new(|| CapsFeatures::new(&[*CAPS_FEATURE_MEMORY_GL_MEMORY]));

View file

@ -9,10 +9,10 @@
use gst_gl_sys;
use std::ffi::CStr;
lazy_static! {
pub static ref GL_DISPLAY_CONTEXT_TYPE: &'static str = unsafe {
use once_cell::sync::Lazy;
pub static GL_DISPLAY_CONTEXT_TYPE: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_gl_sys::GST_GL_DISPLAY_CONTEXT_TYPE)
.to_str()
.unwrap()
};
}
});

View file

@ -10,9 +10,8 @@
#[macro_use]
extern crate bitflags;
extern crate byteorder;
#[macro_use]
extern crate lazy_static;
extern crate libc;
extern crate once_cell;
#[macro_use]
extern crate glib;
extern crate glib_sys;

View file

@ -15,7 +15,7 @@ build = "build.rs"
[dependencies]
bitflags = "1.0"
libc = "0.2"
lazy_static = "1.0"
once_cell = "1.0"
glib-sys = { git = "https://github.com/gtk-rs/sys" }
gio-sys = { git = "https://github.com/gtk-rs/sys" }
gobject-sys = { git = "https://github.com/gtk-rs/sys" }

View file

@ -8,9 +8,8 @@
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate lazy_static;
extern crate libc;
extern crate once_cell;
extern crate gio;
extern crate gio_sys as gio_sys;
@ -71,63 +70,63 @@ pub use rtsp_stream_transport::RTSPStreamTransportExtManual;
pub use rtsp_context::*;
pub use rtsp_token::*;
lazy_static! {
pub static ref RTSP_ADDRESS_POOL_ANY_IPV4: &'static str = unsafe {
use once_cell::sync::Lazy;
pub static RTSP_ADDRESS_POOL_ANY_IPV4: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_ADDRESS_POOL_ANY_IPV4)
.to_str()
.unwrap()
};
pub static ref RTSP_ADDRESS_POOL_ANY_IPV6: &'static str = unsafe {
});
pub static RTSP_ADDRESS_POOL_ANY_IPV6: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_ADDRESS_POOL_ANY_IPV6)
.to_str()
.unwrap()
};
pub static ref RTSP_AUTH_CHECK_CONNECT: &'static str = unsafe {
});
pub static RTSP_AUTH_CHECK_CONNECT: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_AUTH_CHECK_CONNECT)
.to_str()
.unwrap()
};
pub static ref RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS: &'static str = unsafe {
});
pub static RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS)
.to_str()
.unwrap()
};
pub static ref RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT: &'static str = unsafe {
});
pub static RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT)
.to_str()
.unwrap()
};
pub static ref RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS: &'static str = unsafe {
});
pub static RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS)
.to_str()
.unwrap()
};
pub static ref RTSP_AUTH_CHECK_URL: &'static str = unsafe {
});
pub static RTSP_AUTH_CHECK_URL: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_AUTH_CHECK_URL)
.to_str()
.unwrap()
};
pub static ref RTSP_PERM_MEDIA_FACTORY_ACCESS: &'static str = unsafe {
});
pub static RTSP_PERM_MEDIA_FACTORY_ACCESS: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_PERM_MEDIA_FACTORY_ACCESS)
.to_str()
.unwrap()
};
pub static ref RTSP_PERM_MEDIA_FACTORY_CONSTRUCT: &'static str = unsafe {
});
pub static RTSP_PERM_MEDIA_FACTORY_CONSTRUCT: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT)
.to_str()
.unwrap()
};
pub static ref RTSP_TOKEN_MEDIA_FACTORY_ROLE: &'static str = unsafe {
});
pub static RTSP_TOKEN_MEDIA_FACTORY_ROLE: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE)
.to_str()
.unwrap()
};
pub static ref RTSP_TOKEN_TRANSPORT_CLIENT_SETTINGS: &'static str = unsafe {
});
pub static RTSP_TOKEN_TRANSPORT_CLIENT_SETTINGS: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_rtsp_server_sys::GST_RTSP_TOKEN_TRANSPORT_CLIENT_SETTINGS)
.to_str()
.unwrap()
};
}
});
// Re-export all the traits in a prelude module, so that applications
// can always "use gst::prelude::*" without getting conflicts

View file

@ -23,7 +23,7 @@ gstreamer-video-sys = { git = "https://gitlab.freedesktop.org/gstreamer/gstreame
glib = { git = "https://github.com/gtk-rs/glib" }
gstreamer = { path = "../gstreamer" }
gstreamer-base = { path = "../gstreamer-base" }
lazy_static = "1.0"
once_cell = "1.0"
[build-dependencies]
rustdoc-stripper = { version = "0.1", optional = true }

View file

@ -8,9 +8,8 @@
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate lazy_static;
extern crate libc;
extern crate once_cell;
#[macro_use]
extern crate glib;

View file

@ -12,28 +12,30 @@ use std::mem;
use glib::translate::*;
lazy_static! {
pub static ref BUFFER_POOL_OPTION_VIDEO_AFFINE_TRANSFORMATION_META: &'static str = unsafe {
use once_cell::sync::Lazy;
pub static BUFFER_POOL_OPTION_VIDEO_AFFINE_TRANSFORMATION_META: Lazy<&'static str> =
Lazy::new(|| unsafe {
CStr::from_ptr(gst_video_sys::GST_BUFFER_POOL_OPTION_VIDEO_AFFINE_TRANSFORMATION_META)
.to_str()
.unwrap()
};
pub static ref BUFFER_POOL_OPTION_VIDEO_ALIGNMENT: &'static str = unsafe {
});
pub static BUFFER_POOL_OPTION_VIDEO_ALIGNMENT: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_video_sys::GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)
.to_str()
.unwrap()
};
pub static ref BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META: &'static str = unsafe {
});
pub static BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META: Lazy<&'static str> =
Lazy::new(|| unsafe {
CStr::from_ptr(gst_video_sys::GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META)
.to_str()
.unwrap()
};
pub static ref BUFFER_POOL_OPTION_VIDEO_META: &'static str = unsafe {
});
pub static BUFFER_POOL_OPTION_VIDEO_META: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_video_sys::GST_BUFFER_POOL_OPTION_VIDEO_META)
.to_str()
.unwrap()
};
}
});
#[derive(Debug, Clone)]
pub struct VideoAlignment(pub(crate) gst_video_sys::GstVideoAlignment);

View file

@ -21,7 +21,7 @@ gobject-sys = { git = "https://github.com/gtk-rs/sys" }
gstreamer-sys = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs-sys", features = ["v1_8"] }
glib = { git = "https://github.com/gtk-rs/glib" }
num-rational = { version = "0.2", default-features = false, features = [] }
lazy_static = "1.0"
once_cell = "1.0"
futures-core = "0.3"
futures-util = "0.3"
futures-channel = "0.3"

View file

@ -878,12 +878,10 @@ impl<T> Eq for MappedBuffer<T> {}
unsafe impl<T> Send for MappedBuffer<T> {}
unsafe impl<T> Sync for MappedBuffer<T> {}
lazy_static! {
pub static ref BUFFER_COPY_METADATA: ::BufferCopyFlags =
::BufferCopyFlags::FLAGS | ::BufferCopyFlags::TIMESTAMPS | ::BufferCopyFlags::META;
pub static ref BUFFER_COPY_ALL: ::BufferCopyFlags =
*BUFFER_COPY_METADATA | ::BufferCopyFlags::MEMORY;
}
pub const BUFFER_COPY_METADATA: ::BufferCopyFlags =
::BufferCopyFlags::from_bits_truncate(gst_sys::GST_BUFFER_COPY_METADATA);
pub const BUFFER_COPY_ALL: ::BufferCopyFlags =
::BufferCopyFlags::from_bits_truncate(gst_sys::GST_BUFFER_COPY_ALL);
#[cfg(test)]
mod tests {

View file

@ -15,6 +15,8 @@ use std::ops::{Deref, DerefMut};
use std::ptr;
use std::str;
use once_cell::sync::Lazy;
use glib;
use glib::translate::{
from_glib, from_glib_full, from_glib_none, FromGlibPtrFull, FromGlibPtrNone, GlibPtrDefault,
@ -461,15 +463,13 @@ impl ToOwned for CapsFeaturesRef {
unsafe impl Sync for CapsFeaturesRef {}
unsafe impl Send for CapsFeaturesRef {}
lazy_static! {
pub static ref CAPS_FEATURE_MEMORY_SYSTEM_MEMORY: &'static str = unsafe {
pub static CAPS_FEATURE_MEMORY_SYSTEM_MEMORY: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_sys::GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY)
.to_str()
.unwrap()
};
pub static ref CAPS_FEATURES_MEMORY_SYSTEM_MEMORY: CapsFeatures =
CapsFeatures::new(&[*CAPS_FEATURE_MEMORY_SYSTEM_MEMORY]);
}
});
pub static CAPS_FEATURES_MEMORY_SYSTEM_MEMORY: Lazy<CapsFeatures> =
Lazy::new(|| CapsFeatures::new(&[*CAPS_FEATURE_MEMORY_SYSTEM_MEMORY]));
#[cfg(test)]
mod tests {

View file

@ -9,6 +9,8 @@
use Element;
use ElementClass;
use once_cell::sync::Lazy;
use glib;
#[cfg(any(feature = "v1_10", feature = "dox"))]
use glib::object::Cast;
@ -845,38 +847,36 @@ impl ElementClass {
}
}
lazy_static! {
pub static ref ELEMENT_METADATA_AUTHOR: &'static str = unsafe {
pub static ELEMENT_METADATA_AUTHOR: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_sys::GST_ELEMENT_METADATA_AUTHOR)
.to_str()
.unwrap()
};
pub static ref ELEMENT_METADATA_DESCRIPTION: &'static str = unsafe {
});
pub static ELEMENT_METADATA_DESCRIPTION: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_sys::GST_ELEMENT_METADATA_DESCRIPTION)
.to_str()
.unwrap()
};
pub static ref ELEMENT_METADATA_DOC_URI: &'static str = unsafe {
});
pub static ELEMENT_METADATA_DOC_URI: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_sys::GST_ELEMENT_METADATA_DOC_URI)
.to_str()
.unwrap()
};
pub static ref ELEMENT_METADATA_ICON_NAME: &'static str = unsafe {
});
pub static ELEMENT_METADATA_ICON_NAME: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_sys::GST_ELEMENT_METADATA_ICON_NAME)
.to_str()
.unwrap()
};
pub static ref ELEMENT_METADATA_KLASS: &'static str = unsafe {
});
pub static ELEMENT_METADATA_KLASS: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_sys::GST_ELEMENT_METADATA_KLASS)
.to_str()
.unwrap()
};
pub static ref ELEMENT_METADATA_LONGNAME: &'static str = unsafe {
});
pub static ELEMENT_METADATA_LONGNAME: Lazy<&'static str> = Lazy::new(|| unsafe {
CStr::from_ptr(gst_sys::GST_ELEMENT_METADATA_LONGNAME)
.to_str()
.unwrap()
};
}
});
#[macro_export]
macro_rules! gst_element_error(

View file

@ -12,9 +12,8 @@ extern crate bitflags;
#[cfg(any(feature = "v1_14", feature = "dox"))]
#[macro_use]
extern crate cfg_if;
#[macro_use]
extern crate lazy_static;
extern crate libc;
extern crate once_cell;
extern crate thiserror;
// Re-exported for the subclass gst_plugin_define! macro

View file

@ -14,6 +14,8 @@ use std::ffi::CStr;
use std::fmt;
use std::ptr;
use once_cell::sync::Lazy;
use gobject_sys;
use gst_sys;
@ -167,20 +169,18 @@ impl fmt::Debug for DebugCategory {
}
}
lazy_static! {
pub static ref CAT_RUST: DebugCategory = DebugCategory::new(
pub static CAT_RUST: Lazy<DebugCategory> = Lazy::new(|| {
DebugCategory::new(
"GST_RUST",
::DebugColorFlags::UNDERLINE,
Some("GStreamer's Rust binding core"),
);
}
)
});
macro_rules! declare_debug_category_from_name(
($cat:ident, $cat_name:expr) => (
lazy_static! {
pub static ref $cat: DebugCategory = DebugCategory::get($cat_name)
.expect(&format!("Unable to find `DebugCategory` with name {}", $cat_name));
}
pub static $cat: Lazy<DebugCategory> = Lazy::new(|| DebugCategory::get($cat_name)
.expect(&format!("Unable to find `DebugCategory` with name {}", $cat_name)));
);
);

View file

@ -11,6 +11,8 @@ use std::fmt;
use std::marker::PhantomData;
use std::mem;
use once_cell::sync::Lazy;
use glib;
use glib::translate::{
from_glib, from_glib_full, FromGlibPtrFull, ToGlib, ToGlibPtr, ToGlibPtrMut,
@ -42,10 +44,8 @@ macro_rules! impl_tag(
}
}
lazy_static! {
pub(crate) static ref $rust_tag: &'static str =
unsafe { CStr::from_ptr(gst_sys::$gst_tag).to_str().unwrap() };
}
pub(crate) static $rust_tag: Lazy<&'static str> = Lazy::new(||
unsafe { CStr::from_ptr(gst_sys::$gst_tag).to_str().unwrap() });
};
);

View file

@ -18,6 +18,8 @@ use serde::ser::{Serialize, SerializeTuple, Serializer};
use std::{fmt, mem};
use once_cell::sync::Lazy;
use Buffer;
use DateTime;
use Sample;
@ -32,20 +34,20 @@ fn get_other_type_id<T: StaticType>() -> usize {
}
}
lazy_static! {
pub(crate) static ref ARRAY_OTHER_TYPE_ID: usize = get_other_type_id::<Array>();
pub(crate) static ref BITMASK_OTHER_TYPE_ID: usize = get_other_type_id::<Bitmask>();
pub(crate) static ref DATE_OTHER_TYPE_ID: usize = get_other_type_id::<Date>();
pub(crate) static ref DATE_TIME_OTHER_TYPE_ID: usize = get_other_type_id::<DateTime>();
pub(crate) static ref FRACTION_OTHER_TYPE_ID: usize = get_other_type_id::<Fraction>();
pub(crate) static ref FRACTION_RANGE_OTHER_TYPE_ID: usize =
get_other_type_id::<FractionRange>();
pub(crate) static ref INT_RANGE_I32_OTHER_TYPE_ID: usize = get_other_type_id::<IntRange<i32>>();
pub(crate) static ref INT_RANGE_I64_OTHER_TYPE_ID: usize = get_other_type_id::<IntRange<i64>>();
pub(crate) static ref LIST_OTHER_TYPE_ID: usize = get_other_type_id::<List>();
pub(crate) static ref SAMPLE_OTHER_TYPE_ID: usize = get_other_type_id::<Sample>();
pub(crate) static ref BUFFER_OTHER_TYPE_ID: usize = get_other_type_id::<Buffer>();
}
pub(crate) static ARRAY_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<Array>);
pub(crate) static BITMASK_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<Bitmask>);
pub(crate) static DATE_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<Date>);
pub(crate) static DATE_TIME_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<DateTime>);
pub(crate) static FRACTION_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<Fraction>);
pub(crate) static FRACTION_RANGE_OTHER_TYPE_ID: Lazy<usize> =
Lazy::new(get_other_type_id::<FractionRange>);
pub(crate) static INT_RANGE_I32_OTHER_TYPE_ID: Lazy<usize> =
Lazy::new(get_other_type_id::<IntRange<i32>>);
pub(crate) static INT_RANGE_I64_OTHER_TYPE_ID: Lazy<usize> =
Lazy::new(get_other_type_id::<IntRange<i64>>);
pub(crate) static LIST_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<List>);
pub(crate) static SAMPLE_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<Sample>);
pub(crate) static BUFFER_OTHER_TYPE_ID: Lazy<usize> = Lazy::new(get_other_type_id::<Buffer>);
impl<'a> Serialize for Fraction {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {