Skip to content

Commit

Permalink
#7 ctors.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 17, 2023
1 parent 7b68f51 commit d355985
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
44 changes: 44 additions & 0 deletions src/ctors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{Map, Pair};
use std::mem::MaybeUninit;

impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
/// Make it.
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
next: 0,
pairs: unsafe { *MaybeUninit::<[Pair<K, V>; N]>::uninit().as_mut_ptr() },
}
}
}

#[cfg(test)]
use anyhow::Result;

#[test]
fn makes_new_map() -> Result<()> {
let m: Map<u8, u8, 8> = Map::new();
assert_eq!(0, m.len());
Ok(())
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#![allow(clippy::multiple_inherent_impl)]
#![allow(clippy::multiple_crate_versions)]

mod ctors;
mod debug;
mod iterators;
mod map;
Expand Down
13 changes: 1 addition & 12 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
// SOFTWARE.

use crate::Pair::{Absent, Present};
use crate::{IntoIter, Iter, Map, Pair};
use std::mem::MaybeUninit;
use crate::{IntoIter, Iter, Map};

impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Default for Map<K, V, N> {
fn default() -> Self {
Expand All @@ -29,16 +28,6 @@ impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Default for Map<K, V,
}

impl<K: Copy + PartialEq, V: Clone + Copy, const N: usize> Map<K, V, N> {
/// Make it.
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
next: 0,
pairs: unsafe { *MaybeUninit::<[Pair<K, V>; N]>::uninit().as_mut_ptr() },
}
}

/// Make an iterator over all pairs.
#[inline]
#[must_use]
Expand Down

0 comments on commit d355985

Please sign in to comment.