"proxmox-api-macro",
"proxmox-borrow",
"proxmox-http",
+ "proxmox-lang",
"proxmox-sortable-macro",
"proxmox-tfa",
"proxmox-time",
proxmox-api-macro \
proxmox-borrow \
proxmox-http \
+ proxmox-lang \
proxmox-sortable-macro \
proxmox-tfa \
proxmox-time \
--- /dev/null
+[package]
+name = "proxmox-lang"
+version = "1.0.0"
+authors = ["Proxmox Support Team <support@proxmox.com>"]
+edition = "2018"
+license = "AGPL-3"
+description = "simple rust language utilities such as try_block, which have no dependencies"
+
+exclude = [ "debian" ]
+
+[dependencies]
--- /dev/null
+rust-proxmox-lang (1.0.0-1) stable; urgency=medium
+
+ * initial split out of `librust-proxmox-dev`
+
+ -- Proxmox Support Team <support@proxmox.com> Wed, 06 Oct 2021 11:04:36 +0200
--- /dev/null
+Source: rust-proxmox-lang
+Section: rust
+Priority: optional
+Build-Depends: debhelper (>= 12),
+ dh-cargo (>= 24),
+ cargo:native <!nocheck>,
+ rustc:native <!nocheck>,
+ libstd-rust-dev <!nocheck>
+Maintainer: Proxmox Support Team <support@proxmox.com>
+Standards-Version: 4.5.1
+Vcs-Git: git://git.proxmox.com/git/proxmox.git
+Vcs-Browser: https://git.proxmox.com/?p=proxmox.git
+Rules-Requires-Root: no
+
+Package: librust-proxmox-lang-dev
+Architecture: any
+Multi-Arch: same
+Depends:
+ ${misc:Depends}
+Provides:
+ librust-proxmox-lang+default-dev (= ${binary:Version}),
+ librust-proxmox-lang-1-dev (= ${binary:Version}),
+ librust-proxmox-lang-1+default-dev (= ${binary:Version}),
+ librust-proxmox-lang-1.0-dev (= ${binary:Version}),
+ librust-proxmox-lang-1.0+default-dev (= ${binary:Version}),
+ librust-proxmox-lang-1.0.0-dev (= ${binary:Version}),
+ librust-proxmox-lang-1.0.0+default-dev (= ${binary:Version})
+Description: Simple rust language utilities such as try_block, which have no dependencies - Rust source code
+ This package contains the source for the Rust proxmox-lang crate, packaged by
+ debcargo for use with cargo and dh-cargo.
--- /dev/null
+Copyright (C) 2021 Proxmox Server Solutions GmbH
+
+This software is written by Proxmox Server Solutions GmbH <support@proxmox.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
--- /dev/null
+overlay = "."
+crate_src_path = ".."
+maintainer = "Proxmox Support Team <support@proxmox.com>"
+
+[source]
+vcs_git = "git://git.proxmox.com/git/proxmox.git"
+vcs_browser = "https://git.proxmox.com/?p=proxmox.git"
--- /dev/null
+/// A macro to generate a list of pub const variabales, and
+/// an accompaning static array of a given name, the values are automatically
+/// assigned to a bit (with doc comments)
+///
+/// Example:
+/// ```
+/// # use proxmox_lang::constnamedbitmap;
+///
+/// constnamedbitmap! {
+/// /// A list of privileges
+/// PRIVS: u64 => {
+/// /// Some comment for Priv1
+/// PRIV1("Priv1");
+/// PRIV2("Priv2");
+/// PRIV3("Priv3");
+/// }
+/// }
+/// # assert!(PRIV1 == 1<<0);
+/// # assert!(PRIV2 == 1<<1);
+/// # assert!(PRIV3 == 1<<2);
+/// ```
+///
+/// this will generate the following variables:
+/// ```
+/// /// Some comment for Priv1
+/// pub const PRIV1: u64 = 1;
+/// pub const PRIV2: u64 = 2;
+/// pub const PRIV3: u64 = 4;
+///
+/// /// A list of privileges
+/// pub const PRIVS: &[(&str, u64)] = &[
+/// ("Priv1", PRIV1),
+/// ("Priv2", PRIV2),
+/// ("Priv3", PRIV3),
+/// ];
+/// ```
+#[macro_export(local_inner_macros)]
+macro_rules! constnamedbitmap {
+ (
+ $(#[$outer:meta])*
+ $name:ident : $type:ty => {
+ $($content:tt)+
+ }
+ ) => {
+ __constnamemap_consts! {
+ ($type) (0) => $($content)+
+ }
+
+ $(#[$outer])*
+ pub const $name: &[(&str, $type)] =
+ __constnamemap_entries! {
+ $($content)+
+ };
+ }
+}
+
+#[doc(hidden)]
+#[macro_export(local_inner_macros)]
+macro_rules! __constnamemap_consts {
+ (($type:ty) ($counter:expr) => ) => {};
+ (
+ ($type:ty) ($counter:expr) =>
+ $(#[$outer:meta])*
+ $name:ident($text:expr);
+ $(
+ $content:tt
+ )*
+ ) => {
+ $(#[$outer])*
+ pub const $name: $type = 1 << ($counter);
+ __constnamemap_consts! {
+ ($type) (1+$counter) => $($content)*
+ }
+ }
+}
+
+#[doc(hidden)]
+#[macro_export(local_inner_macros)]
+macro_rules! __constnamemap_entries {
+ (
+ $(
+ $(#[$outer:meta])*
+ $name:ident($text:expr);
+ )*
+ ) => {
+ &[
+ $(($text,$name),)*
+ ]
+ }
+}
--- /dev/null
+//! Rust language related helpers.
+//!
+//! This provides some macros for features which are not yet available in the language, or
+//! sometimes also types from nightly `std` which are simple enough to do just haven't been
+//! bikeshedded and stabilized in the standard library yet.
+
+mod constnamedbitmap;
+
+pub mod ops;
+
+/// Macro to write error-handling blocks (like perl eval {})
+///
+/// #### Example:
+/// ```
+/// # use proxmox_lang::try_block;
+/// # macro_rules! format_err {
+/// # ($($msg:tt)+) => { format!($($msg)+) }
+/// # }
+/// # macro_rules! bail {
+/// # ($($msg:tt)+) => { return Err(format_err!($($msg)+)); }
+/// # }
+/// # let some_condition = false;
+/// let result = try_block!({
+/// if (some_condition) {
+/// bail!("some error");
+/// }
+/// Ok(())
+/// })
+/// .map_err(|e| format_err!("my try block returned an error - {}", e));
+/// ```
+
+#[macro_export]
+macro_rules! try_block {
+ { $($token:tt)* } => {{ (|| -> Result<_,_> { $($token)* })() }}
+}
+
+/// Statically assert the size of a type at compile time.
+///
+/// This should compile:
+/// ```
+/// # use proxmox_lang::static_assert_size;
+/// #[repr(C)]
+/// struct Stuff {
+/// value: [u8; 32]
+/// }
+/// static_assert_size!(Stuff, 32);
+/// ```
+///
+/// This should fail to compile:
+/// ```compile_fail
+/// # use proxmox_lang::static_assert_size;
+/// #[repr(C)]
+/// struct Stuff {
+/// value: [u8; 32]
+/// }
+/// static_assert_size!(Stuff, 128);
+/// ```
+#[macro_export]
+macro_rules! static_assert_size {
+ ($ty:ty, $size:expr) => {
+ const _: fn() -> () = || {
+ let _ = ::std::mem::transmute::<[u8; $size], $ty>;
+ };
+ };
+}
+
+/// Evaluates to the offset (in bytes) of a given member within a struct
+///
+/// ```
+/// # use proxmox_lang::offsetof;
+///
+/// #[repr(C)]
+/// struct Stuff {
+/// first: u32,
+/// second: u32,
+/// }
+///
+/// assert_eq!(offsetof!(Stuff, second), 4);
+///
+/// ```
+// FIXME: With 1.56 we get `const transmute` which may help making this usable `const fn` as we can
+// avoid dereferencing the raw pointer by transmuting `0usize` into a proper reference instead.
+//
+// So with 1.56, do this instead:
+//
+// unsafe { &(std::mem::transmute::<_, &$ty>(0usize).$field) as *const _ as usize }
+#[macro_export]
+macro_rules! offsetof {
+ ($ty:ty, $field:ident) => {
+ unsafe { &(*(std::ptr::null::<$ty>())).$field as *const _ as usize }
+ };
+}
--- /dev/null
+//! std::ops extensions
+
+/// Modeled after the nightly `std::ops::ControlFlow`.
+///
+/// To be removed with rust 1.55.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum ControlFlow<B, C = ()> {
+ Continue(C),
+ Break(B),
+}
+
+impl<B> ControlFlow<B> {
+ pub const CONTINUE: ControlFlow<B, ()> = ControlFlow::Continue(());
+}
+++ /dev/null
-/// A macro to generate a list of pub const variabales, and
-/// an accompaning static array of a given name, the values are automatically
-/// assigned to a bit (with doc comments)
-///
-/// Example:
-/// ```
-/// # use proxmox::constnamedbitmap;
-///
-/// constnamedbitmap! {
-/// /// A list of privileges
-/// PRIVS: u64 => {
-/// /// Some comment for Priv1
-/// PRIV1("Priv1");
-/// PRIV2("Priv2");
-/// PRIV3("Priv3");
-/// }
-/// }
-/// # assert!(PRIV1 == 1<<0);
-/// # assert!(PRIV2 == 1<<1);
-/// # assert!(PRIV3 == 1<<2);
-/// ```
-///
-/// this will generate the following variables:
-/// ```
-/// /// Some comment for Priv1
-/// pub const PRIV1: u64 = 1;
-/// pub const PRIV2: u64 = 2;
-/// pub const PRIV3: u64 = 4;
-///
-/// /// A list of privileges
-/// pub const PRIVS: &[(&str, u64)] = &[
-/// ("Priv1", PRIV1),
-/// ("Priv2", PRIV2),
-/// ("Priv3", PRIV3),
-/// ];
-/// ```
-#[macro_export(local_inner_macros)]
-macro_rules! constnamedbitmap {
- (
- $(#[$outer:meta])*
- $name:ident : $type:ty => {
- $($content:tt)+
- }
- ) => {
- __constnamemap_consts! {
- ($type) (0) => $($content)+
- }
-
- $(#[$outer])*
- pub const $name: &[(&str, $type)] =
- __constnamemap_entries! {
- $($content)+
- };
- }
-}
-
-#[doc(hidden)]
-#[macro_export(local_inner_macros)]
-macro_rules! __constnamemap_consts {
- (($type:ty) ($counter:expr) => ) => {};
- (
- ($type:ty) ($counter:expr) =>
- $(#[$outer:meta])*
- $name:ident($text:expr);
- $(
- $content:tt
- )*
- ) => {
- $(#[$outer])*
- pub const $name: $type = 1 << ($counter);
- __constnamemap_consts! {
- ($type) (1+$counter) => $($content)*
- }
- }
-}
-
-#[doc(hidden)]
-#[macro_export(local_inner_macros)]
-macro_rules! __constnamemap_entries {
- (
- $(
- $(#[$outer:meta])*
- $name:ident($text:expr);
- )*
- ) => {
- &[
- $(($text,$name),)*
- ]
- }
-}
pub mod as_any;
pub mod byte_buffer;
pub mod common_regex;
-pub mod constnamedbitmap;
pub mod email;
pub mod fd;
pub mod fs;
#[doc(inline)]
pub use as_any::AsAny;
-/// Evaluates to the offset (in bytes) of a given member within a struct
-#[macro_export]
-macro_rules! offsetof {
- ($ty:ty, $field:ident) => {
- unsafe { &(*(std::ptr::null::<$ty>())).$field as *const _ as usize }
- };
-}
-
-/// Statically assert the size of a type at compile time.
-///
-/// This should compile:
-/// ```
-/// # use proxmox::static_assert_size;
-/// #[repr(C)]
-/// struct Stuff {
-/// value: [u8; 32]
-/// }
-/// static_assert_size!(Stuff, 32);
-/// ```
-///
-/// This should fail to compile:
-/// ```compile_fail
-/// # use proxmox::static_assert_size;
-/// #[repr(C)]
-/// struct Stuff {
-/// value: [u8; 32]
-/// }
-/// static_assert_size!(Stuff, 128);
-/// ```
-#[macro_export]
-macro_rules! static_assert_size {
- ($ty:ty, $size:expr) => {
- const _: fn() -> () = || {
- let _ = ::std::mem::transmute::<[u8; $size], $ty>;
- };
- };
-}
-
-/// Macro to write error-handling blocks (like perl eval {})
-///
-/// #### Example:
-/// ```
-/// # use proxmox::try_block;
-/// # use anyhow::*;
-/// # let some_condition = false;
-/// let result = try_block!({
-/// if (some_condition) {
-/// bail!("some error");
-/// }
-/// Ok(())
-/// })
-/// .map_err(|e| format_err!("my try block returned an error - {}", e));
-/// ```
-
-#[macro_export]
-macro_rules! try_block {
- { $($token:tt)* } => {{ (|| -> Result<_,_> { $($token)* })() }}
-}
-
const HEX_CHARS: &[u8; 16] = b"0123456789abcdef";
/// Helper to provide a `Display` for arbitrary byte slices.