iceoryx2_bb_posix/
creation_mode.rs

1// Copyright (c) 2023 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13//! The [`CreationMode`] describes how certain posix resources should be created.
14
15use core::fmt::Display;
16use iceoryx2_pal_posix::*;
17
18/// Describes how new resources like [`crate::file::File`], [`crate::shared_memory::SharedMemory`]
19/// or others should be created.
20#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Default)]
21pub enum CreationMode {
22    /// Create resource, if its already existing fail.
23    #[default]
24    CreateExclusive,
25    /// Always remove existing resource and override it with new one
26    PurgeAndCreate,
27    /// Either open the new resource or create it when it is not existing
28    OpenOrCreate,
29}
30
31impl CreationMode {
32    pub fn as_oflag(&self) -> posix::int {
33        match self {
34            CreationMode::PurgeAndCreate => posix::O_CREAT | posix::O_EXCL,
35            CreationMode::CreateExclusive => posix::O_CREAT | posix::O_EXCL,
36            CreationMode::OpenOrCreate => posix::O_CREAT,
37        }
38    }
39}
40
41impl Display for CreationMode {
42    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43        write!(
44            f,
45            "CreationMode::{}",
46            match self {
47                CreationMode::CreateExclusive => "CreateExclusive",
48                CreationMode::PurgeAndCreate => "PurgeAndCreate",
49                CreationMode::OpenOrCreate => "OpenOrCreate",
50            }
51        )
52    }
53}